none
Logical AND in XPath

    Question

  • Hello,

     

    I am using XPath 1.0 to get the nodes from the XML file.

     

    Sample.XML

     

    <root>

    <message cid = "200" bid = "290" aid = "2">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "2098" aid = "20">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "209" aid = "20">

    <values>Some Value</values>

    </message>

    </root>

     

    What I want is to select the node with (cid = "200" and bid = "209"). How can I do this in XPath? I think I need to apply Logical AND here but I dont get it working in C#.

     

    Any ideas???

    Wednesday, September 26, 2007 8:36 AM

Answers

  •  

    cid and bid are attributes of the message elements which are children of the root element so you need an XPath expression alike

    /root/message[@cid = '200' and @bid = '209']

    to select those message elements or perhaps

    /root/message[@cid = '200' and @bid = '209']/values

    to select the values child element(s).

    Wednesday, September 26, 2007 1:03 PM
  •  AProgrammer wrote:

    Hello,

     

    I am using XPath 1.0 to get the nodes from the XML file.

     

    Sample.XML

     

    <root>

    <message cid = "200" bid = "290" aid = "2">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "2098" aid = "20">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "209" aid = "20">

    <values>Some Value</values>

    </message>

    </root>

     

    What I want is to select the node with (cid = "200" and bid = "209"). How can I do this in XPath? I think I need to apply Logical AND here but I dont get it working in C#.

     

    Any ideas???

     

    The logical "and" operator in XPath is simply "and".

     

    One XPath expression that selects the wanted node is:

     

    /*/message[@cid = "200" and @bid = "209" ]

     

    In .NET one may create an XPathdocument and an XPathNavigator from it and then use the Evaluate() method, or one may create an XmlDocument and use its SelectNodes() method.

     

    Hope this helped.

     

    Cheers,

    Dimitre Novatchev

    Wednesday, September 26, 2007 1:13 PM

All replies

  •  

    cid and bid are attributes of the message elements which are children of the root element so you need an XPath expression alike

    /root/message[@cid = '200' and @bid = '209']

    to select those message elements or perhaps

    /root/message[@cid = '200' and @bid = '209']/values

    to select the values child element(s).

    Wednesday, September 26, 2007 1:03 PM
  •  AProgrammer wrote:

    Hello,

     

    I am using XPath 1.0 to get the nodes from the XML file.

     

    Sample.XML

     

    <root>

    <message cid = "200" bid = "290" aid = "2">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "2098" aid = "20">

    <values>Some Value</values>

    </message>

    <message cid = "200" bid = "209" aid = "20">

    <values>Some Value</values>

    </message>

    </root>

     

    What I want is to select the node with (cid = "200" and bid = "209"). How can I do this in XPath? I think I need to apply Logical AND here but I dont get it working in C#.

     

    Any ideas???

     

    The logical "and" operator in XPath is simply "and".

     

    One XPath expression that selects the wanted node is:

     

    /*/message[@cid = "200" and @bid = "209" ]

     

    In .NET one may create an XPathdocument and an XPathNavigator from it and then use the Evaluate() method, or one may create an XmlDocument and use its SelectNodes() method.

     

    Hope this helped.

     

    Cheers,

    Dimitre Novatchev

    Wednesday, September 26, 2007 1:13 PM
  • Thanks Martin and Dimitre...

    Wednesday, September 26, 2007 2:06 PM