How to create XPath query expression on two nodes?

Posted on 2008-11-14
Last Modified: 2012-05-05
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 ...
Question by:koleto115
    LVL 6

    Expert Comment

     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);

    Author Comment

    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
    LVL 6

    Expert Comment

    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);

    Author Comment

    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.

    LVL 6

    Accepted Solution

    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


    Author Comment

    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
    LVL 6

    Expert Comment

    Do you want to post the actual XML?

    Author Comment

    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 ...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
    zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video discusses moving either the default database or any database to a new volume.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now