• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 877
  • Last Modified:

How to create XPath query expression on two nodes?

I have an input xml like:


I need a only the XmlNodeList of all Result nodes depending on the two nodes cID and sID and
I have try a query like this:

 string match = string.Format("/DataResults/DataAnalysis[cID = '{0}' and sID = '{1}']", CID,SID);
 XmlNodeList results = doc.DocumentElement.SelectNodes(match);

What is wrong with that?
This is under Compact Framework ...
  • 4
  • 4
1 Solution
 string match = string.Format("Event/DataResults/DataAnalysis[cID = '{0}' and sID = '{1}']", CID,SID);


 string match = string.Format("//DataResults/DataAnalysis[cID = '{0}' and sID = '{1}']", CID,SID);
koleto115Author Commented:
No luck!
Both solutions are not working.
The second one throws exception ...

I try both of them with:
I will remove now the DocumentElement from the call
I did notice that the xml your provided was invalid:

You don't open the element <AnalysisResults> you only close it.

I think maybe you need

XmlNodeList results = doc.SelectNodes(match);
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

koleto115Author Commented:
Sorry It is just example xml and a made meistake on it AnalysisResults =is actually DataResults.
I modify the real names of the xml nodes.

The problem is still there after I removed the DocumentElement and did the query directly on the doc both of the solution are throwing exception.

The following works for me:
XmlDocument doc = new XmlDocument();
            + "<DataAnalysis><sID>5</sID><cID>4</cID></DataAnalysis>"
            + "<DataAnalysis><sID>5</sID><cID>3</cID></DataAnalysis>"
            + "</DataResults></Event>");
int CID = 3;
int SID = 5;
XmlNodeList nodes = doc.SelectNodes(string.Format("Event/DataResults/DataAnalysis[cID = '{0}' and sID = '{1}']", CID,SID));
Console.Write(nodes.Count);  //Returns 2

Open in new window

koleto115Author Commented:
It is very strange it works with the sample code but not with the real  xml.
I think the problem is not on the XPath query ...

Anyway I have to look on the original xml
Do you want to post the actual XML?
koleto115Author Commented:
No, I cannot! But I found the mistake :-)
I load a wrong file that does not contains any of these nodes ... How can I be so stupid!!!
I think I will try to become somehow rich and will stop working ...

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now