Load function is already defined in xmlData class
public class XmlData
{
public void Load(XElement xDoc)
{
var id = xDoc.XPathSelectElements("//ID");
var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");
}
}
I'm just calling the Load function from my end.
XmlData aXmlData = new XmlData();
string input, stringXML = "";
TextReader aTextReader = new StreamReader("D:\\test.xml");
while ((input = aTextReader.ReadLine()) != null)
{
stringXML += input;
}
XElement Content = XElement.Parse(stringXML);
aXmlData.Load(Content);
in load function,im getting both id and and listIds as null.
My test.xml contains
<SEARCH>
<ID>11242</ID>
<Lists>
<List CURRENT="true" AGGREGATEDCHANGED="false">
<ListIDS>
<ListID>100567</ListID>
<ListID>100564</ListID>
<ListID>100025</ListID>
<ListID>2</ListID>
<ListID>1</ListID>
</ListIDS>
</List>
</Lists>
</SEARCH>
I am looking at the question 3 hours after it was submitted and 41 minutes after it was (last) edited.
There are no namespaces defined in the provided XML document.
This XPath expression obviously doesn't select any node from the provided XML document, because the XML document doesn't have a top element named
Lists
(the name of the actual top element isSEARCH
)This statement is false, because
//ID
selects the only element named ID in the provided XML document, thus the value of the C# variableid
is non-null. Probably you didn't test thoroughly after editing the XML document.Most probably the original
ID
element belonged to some namespace. But now it is in "no namespace" and the XPath expression above does select it.EDIT: Your sample XML doesn't have an
id
element in the namespace with thenss
alias. It would be<nss:id>
in that case, or there'd be a default namespace set up. I've assumed for this answer that in reality the element you're looking for is in the namespace.Your query is trying to find an element called
id
at the root level. To find allid
elements, you need:... although personally I'd use:
(I prefer using the query methods on LINQ to XML types instead of XPath... I find it easier to avoid silly syntax errors etc.)
Your sample code is also unclear as you're using
xDoc
which hasn't been declared... it helps to write complete examples, ideally including everything required to compile and run as a console app.The reason for the null value or system returned value is due to the following
XpathSElectElements is System.xml.linq.XElment which is linq queried date. It cannot be directly outputed as such. To Get individual first match element use XPathSelectElement("//ID"); You can check the number of occurrences using XPathSelectElements as
you can also query the linq statement as order by using specific conditions
Inorder to get node value from a list u can use this
For Second list you havnt got the value since, the XPath for list items is not correct