Results 1 to 9 of 9

Thread: XPath

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    XPath

    trying to extract all the child nodes where the rootnode is called "Abc"

    xml:

    <role>
    <name>"ABC"</name>
    <values id=1>
    <value>123</value>
    <stuff>asdasdasd</stuff>
    </values>
    <values id=2>
    <value>123</value>
    <stuff>asdfdffsdfsdasdasd</stuff>
    </values>
    </role>


    how can I use XPath to return me all nodes from a root node called "ABC"?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: XPath

    Assuming role is under the root node and the nodes you want to select...


    "/role[name=ABC]"

    Haven't tested it, but that's my guess..

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: XPath

    Just need some quotes around ABC.

    //role[name='ABC']

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: XPath

    hmm. still need to get a hang of this. Actually i mis typed the xml doc. should be:

    <someRootNode>
    <role>
    <name>"ABC"</name>
    <values id=1>
    <value>123</value>
    <stuff>asdasdasd</stuff>
    </values>
    <values id=2>
    <value>123</value>
    <stuff>asdfdffsdfsdasdasd</stuff>
    </values>
    </role>

    </someRootNode>

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: XPath

    That's what we were assuming. The above should work for that.
    c# Code:
    1. XmlDocument xd = new XmlDocument();
    2. xd.Load(@"c:\test\test.xml");
    3.  
    4. XmlNode nod = xd.SelectSingleNode("//role[name='ABC']");
    5. if (nod != null)
    6. {
    7.     MessageBox.Show(nod.InnerXml);
    8. }

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: XPath

    what about selecting ALL nodes where the node name is "ABC" - i want to select all nodes in that node

    so..

    <someRootNode>

    <role>
    <name>"ABC"</name>
    <values id=1>
    <value>123</value>
    <stuff>asdasdasd</stuff>
    </values>
    <values id=2>
    <value>123</value>
    <stuff>asdfdffsdfsdasdasd</stuff>
    </values>
    </role>

    </someRootNode>


    should get me the values for valueid1 and valueid2 and valueidX etc...

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: XPath

    So you want to select all <value> nodes where the outtermost containing <role> node contains a <name> node whose value is 'ABC'?

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: XPath

    XmlNodeList nodelist = xd.DocumentElement.SelectNodes("//role/values[value=123]");

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: XPath

    If you want to check for the ABC name first you can try something like this:
    //role[name='ABC']/values

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width