I have some XML and need to be able to read the data within.
A sample of the XML is
<?xml version="1.0" ?>
<ConsumeLeadRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LeadType>Mortgage</LeadType>
<LeadXml>
<ns1:LeadAssigned xmlns:ns1="http://YaddaYadda" xmlns:ns0="http://YaddaYadda" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Lead>
<Reference>1234</Reference>
<Product>
<Mnemonic>Mortgage</Mnemonic>
<Description>Mortgage Leads</Description>
<SubType>Other</SubType>
</Product>
<ExtendedInfo>
<Mortgage>
<MortgageValue>75000</MortgageValue>
<MortgageValueLowerBound>1</MortgageValueLowerBound>
<MortgageValueUpperBound>500</MortgageValueUpperBound>
<PropertyValue>100000</PropertyValue>
<PropertyValueLowerBound>1</PropertyValueLowerBound>
<PropertyValueUpperBound>500</PropertyValueUpperBound>
<Adverse>false</Adverse>
<FirstTime>false</FirstTime>
</Mortgage>
</ExtendedInfo>
<Applicants>
<Applicant1>
<Title>Mr</Title>
<Forename>SampleForename</Forename>
<Surname>SampleSurname</Surname>
<DateOfBirth>1903-02-01</DateOfBirth>
<Smoker>false</Smoker>
<TelephoneNumbers>
<TelephoneNumber>
<Number>01244123456</Number>
<Type>Mobile</Type>
</TelephoneNumber>
</TelephoneNumbers>
<EmailAddresses>
<EmailAddress>
<Email>test#moneysupermarket.com</Email>
<Type>Business</Type>
</EmailAddress>
</EmailAddresses>
<Addresses>
<Address>
<Street>Sample Street</Street>
<District>Sample District</District>
<Town>Sample Town</Town>
<County>Sample County</County>
<Postcode>CH53UZ</Postcode>
<Type>Home</Type>
</Address>
</Addresses>
</Applicant1>
</Applicants>
</ns0:Lead>
<Assignment>
<Price>20</Price>
<AssignmentDateTime>2010-02-01T00:00:00</AssignmentDateTime>
<Subscription>
<Reference>1234</Reference>
<Subscriber>
<Title>Mr</Title>
<Forename>SampleForename</Forename>
<Surname>SampleSurname</Surname>
</Subscriber>
</Subscription>
<Account>
<Reference>1234</Reference>
<CompanyName>Sample Company</CompanyName>
</Account>
<LeadType>SampleLeadType</LeadType>
<TerritoryName>UNITED KINGDOM</TerritoryName>
</Assignment>
</ns1:LeadAssigned>
</LeadXml>
<AuthenticationUsername>Username</AuthenticationUsername>
<AuthenticationPassword>Password</AuthenticationPassword>
</ConsumeLeadRequest>
Using Linq to XML how do i navigate to the items?
Thanks
Sp
I have tried a few things like
XDocument Leads = XDocument.Load(#"C:\Users\Steven.Pentleton\AppData\Local\Temporary Projects\PAALeadImport\PAAExmple.xml");
var Lead = (from L in Leads.Descendants("Lead")
select new { LeadType = (string)L.Element("Reference") }).ToList();
var S = Lead.First();
string T = S.LeadType;
Are you looking for XDcoument or XElement in linq
Namespace: using System.Xml.Linq;
I guess you are looking for a Linq To Xml guide
Related
I have an XML file with a list of parent nodes and childe nodes nested within the parent node, and I need to remove the child nodes when a specific criteria is been met.
Ex: Remove all contact nodes where the id = 1. How can I achieve this using linq and xml. This is my XML structure
<Events>
<Event>
<id>1</id>
<title>AA</title>
<start>2019-12-01T14:13:58.863</start>
<end>2019-12-01T15:13:58.787</end>
<contacts>
<contact>
<id>1</id>
<name>ABC</name>
</contact>
<contact>
<id>2</id>
<name>ABCD</name>
</contact>
<contact>
<id>3</id>
<name>ABCDE</name>
</contact>
</contacts>
</Event>
<Event>
<id>2</id>
<title>BB</title>
<start>2019-12-01T14:13:58.863</start>
<end>2019-12-01T15:13:58.787</end>
<contacts>
<contact>
<id>1</id>
<name>ABC</name>
</contact>
<contact>
<id>2</id>
<name>ABCD</name>
</contact>
<contact>
<id>3</id>
<name>ABCDE</name>
</contact>
</contacts>
</Event>
</Events>
You can get the XML nodes using this query
var query = xmlDoc.Descendants("contact").Where(e => e.Element("id").Value.Equals(id)).ToList();
And then run
query.Remove()
to remove the elements that were returned.
As Jon Skeet pointed out there is no need to do anything esoteric. Here is a complete example how to do it. Pure LINQ to XML.
c#, LINQ to XML
void Main()
{
const string inputXML = #"e:\Temp\MikeOconner.xml";
const string outputXML = #"e:\Temp\MikeOconner_output.xml";
XDocument xml = XDocument.Load(inputXML);
xml.Root.DescendantsAndSelf("contact")
.Where(r => (string)r.Element("id").Value == "1")
.Remove();
xml.Save(outputXML);
}
I would like to parse the below xml using XDocument in Linq.
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<Sources>
<Item>
<Id>1</Id>
<Name>John</Name>
</Item>
<Item>
<Id>2</Id>
<Name>Max</Name>
</Item>
<Item>
<Id>3</Id>
<Name>Ricky</Name>
</Item>
</Sources>
</string>
My parsing code is :
var xDoc = XDocument.Parse(xmlString);
var xElements = xDoc.Element("Sources")?.Elements("Item");
if (xElements != null)
foreach (var source in xElements)
{
Console.Write(source);
}
xElements is always null. I tried using namespace as well, it did not work. How can I resolve this issue?
Try below code:
string stringXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><string xmlns=\"http://tempuri.org/\"><Sources><Item><Id>1</Id><Name>John</Name></Item><Item><Id>2</Id><Name>Max</Name></Item><Item><Id>3</Id><Name>Ricky</Name></Item></Sources></string>";
XDocument xDoc = XDocument.Parse(stringXml);
var items = xDoc.Descendants("{http://tempuri.org/}Sources")?.Descendants("{http://tempuri.org/}Item").ToList();
I tested it and it correctly shows that items has 3 lements :) Maybe you used namespaces differently (it's enough to inspect xDoc objct in object browser and see its namespace).
You need to concatenate the namespace and can directly use Descendants method to fetch all Item nodes like:
XNamespace ns ="http://tempuri.org/";
var xDoc = XDocument.Parse(xmlString);
var xElements = xDoc.Descendants(ns + "Item");
foreach (var source in xElements)
{
Console.Write(source);
}
This prints on Console:
<Item xmlns="http://tempuri.org/">
<Id>1</Id>
<Name>John</Name>
</Item><Item xmlns="http://tempuri.org/">
<Id>2</Id>
<Name>Max</Name>
</Item><Item xmlns="http://tempuri.org/">
<Id>3</Id>
<Name>Ricky</Name>
</Item>
See the working DEMO Fiddle
Sometimes C# driving me crazy, guys :) !
I tried a very simple code:
String pathToXMLFile = openFileDialog2.FileName;
var xdoc = XDocument.Load(pathToXMLFile);
var xElements = xdoc.XPathSelectElements("//NotificationData");
foreach (XElement xe in xElements)
{
Console.WriteLine(xe);
xe.RemoveAll();
}
xdoc.Save("c:/Temp/ReestrZalogovRuManyCollateralItemsSimple_output.xml");
and a very simple XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PledgeNotificationToNotary xmlns="http://fciit.ru/eis2/ruzdi/types/2.2" version="2.2">
<NotificationId>7d8b62c2-7682-43f8-8a2c-ef4296296210</NotificationId>
<NotificationNotaryAddress/>
<NotificationData version="2.2">
<FormUZ1>
<PersonalProperties>
<PersonalProperty>
<OtherProperty>
<Description>Движимое имущество, согласно перечню по договору купли-продажи </Description>
</OtherProperty>
</PersonalProperty>
</PersonalProperties>
<Pledgors>
<Pledgor>
<Organization>
<RussianOrganization>
<NameFull>dgdgdfgdf</NameFull>
<OGRN>35345345</OGRN>
<INN>34534545</INN>
<Address>
<RegionCode>77</RegionCode>
<Region>Москва</Region>
<Street>Малая Семеновская</Street>
<House>fggdgdf</House>
<Building>dfgdfg</Building>
<Apartment>54654646dfg</Apartment>
</Address>
</RussianOrganization>
</Organization>
</Pledgor>
</Pledgors>
<Pledgee>
<Pledgee>
<Organization>
<RussianOrganization>
<NameFull>Тимер Банк (публичное акционерное общество)</NameFull>
<OGRN>1021600000146</OGRN>
<INN>1653016689</INN>
<Address>
<RegionCode>16</RegionCode>
<Region>Республика Татарстан (Татарстан)</Region>
<City>Казань</City>
<Street>проспект Ибрагимова</Street>
<House>58</House>
</Address>
<Email>dfgdgdgdg#timerbank.ru</Email>
</RussianOrganization>
</Organization>
</Pledgee>
</Pledgee>
<PledgeContract>
<Name>ДОГОВОР О ЗАЛОГЕ</Name>
<Date>2018-08-20</Date>
<Number>№ДОКЛЮ/7777/77-7</Number>
<TermOfContract>«01» июня 2028 года</TermOfContract>
</PledgeContract>
<NotificationApplicant>
<Role>2</Role>
<Organization>
<NameFull>Тимер Банк (публичное акционерное общество)</NameFull>
<URN>1021600000146</URN>
<UINN>1653016689</UINN>
<Email>fgdfggsdfgsdfg#timerbank.ru</Email>
</Organization>
<Attorney>
<Name>
<Last>dfgfg</Last>
<First>dfgsdfg</First>
<Middle>sdfgdfg</Middle>
</Name>
<BirthDate>1967-08-01</BirthDate>
<Authority>приказ dgdfgdfgfdg</Authority>
<PersonDocument>
<Code>33</Code>
<Name>Паспорт гражданина Российской Федерации</Name>
<SeriesNumber>2323 232323</SeriesNumber>
</PersonDocument>
<PersonAddress>
<AddressRF registration="true">
<RegionCode>16</RegionCode>
<Region>Республика Татарстан (Татарстан)</Region>
<City>Казань</City>
<Street>gfdgdfg</Street>
<House>343434</House>
</AddressRF>
</PersonAddress>
</Attorney>
</NotificationApplicant>
</FormUZ1>
</NotificationData>
</PledgeNotificationToNotary>
The problem is xdoc.XPathSelectElements in this code doesn't return any Elements.
I thought, may be I haven't yet woken up and tried to test my XML and XPath in the first site in Google for XPath online evulation - https://www.freeformatter.com/xpath-tester.html and my XPath //NotificationData works with this XML like a charm.
But why it doesn't work in C#? :)
Thank you in advance!
I'm trying to read the APP_DATE element in this xml message. Tried using LINQ but unable to read MyResult
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<MyResponse xmlns="http://tempuri.org/">
<MyResult>
<APP_TYPE>I</APP_TYPE>
<APP_NO>152240</APP_NO>
<APP_DATE>10/03/2016</APP_DATE>
</MyResult>
</MyResponse>
</soap:Body>
</soap:Envelope>
Can't be sure because you didn't supply any example code but I assume you struggle because of the namespaces. So try this:
XmlDocument doc = new XmlDocument();
doc.Load("data.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
ns.AddNamespace("x", "http://tempuri.org/");
var result = doc.SelectSingleNode("//soap:Envelope/soap:Body/x:MyResponse/x:MyResult/x:APP_DATE", ns).InnerText;
For deeper understanding you can read this question
Using Linq it will be like this:
var result = XDocument.Load("data.xml").Root
.Descendants(XName.Get("APP_DATE", "http://tempuri.org/"))
.FirstOrDefault()?.Value;
See how in both examples I have to specify the namespaces of what I'm looking for
I have this XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
<game id="123456" name="501">
<player id="1">
<name>john</name>
<score>495</score>
<movesLeft>15</movesLeft>
<won>No</won>
<completed>uncompleted</completed>
</player>
<player id="2">
<name>
konj
</name>
<score>501</score>
<movesLeft>15</movesLeft>
<won>No</won>
<completed>uncompleted</completed>
</player>
</game>
</games>
and I create this query, but it does not compile:
string path = #"D:\xml\dartDatabase.xml";
XElement file = XElement.Load(path);
var query = from f in file.Element("games").Elements("game")
where (string)f.Attribute("id") == "123"
select (string)f.Element("name");
It underlines the first line (around from section), saying the error is:
'Where' not found, are you missing a reference to System.Core.dll or using directive for System.Linq
What's wrong?
Add using System.Linq;