How to create XPath query expression on two nodes?

I have an input xml like:

<Event>
 <DataResults>
    <DataAnalysis>
    <sID>5</sID>
     <cID>3</cID>
     <Result/>
     <Result/>
      ...
  </DataAnalysis>
 </DataResults>
  </AnalysisResults>
</Event>

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 ...
koleto115Asked:
Who is Participating?
 
openshacCommented:
The following works for me:
XmlDocument doc = new XmlDocument();
 
doc.LoadXml("<Event><DataResults><DataAnalysis><sID>5</sID><cID>3</cID></DataAnalysis>" 
            + "<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

0
 
openshacCommented:
Try
 string match = string.Format("Event/DataResults/DataAnalysis[cID = '{0}' and sID = '{1}']", CID,SID);

or

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

I try both of them with:
 doc.DocumentElement.SelectNodes(match);
I will remove now the DocumentElement from the call
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
openshacCommented:
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);
0
 
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.

:-(
0
 
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
Thanks!
0
 
openshacCommented:
Do you want to post the actual XML?
0
 
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 ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.