CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Posts
    40

    XML SelectNodes (Position)

    Is there any way to select a node by its position in an xml document? The example would be something like this:

    <Element Name="Element1">
    <Value Name="Value1"/>
    <Value Name="Value2"/>
    <Value Name="Value3"/>
    </Element>
    <Element Name="Element2">
    <Value Name="Value4"/>
    <Value Name="Value5"/>
    <Value Name="Value6"/>
    </Element>
    <Element Name="Element3">
    <Value Name="Value7"/>
    <Value Name="Value8"/>
    <Value Name="Value9"/>
    </Element>

    And I need to select the third child of every Element. So the returned set of nodes would be Value3,Value6, and Value9. I tried something like this:

    dim xml as string 'set to xml example above
    dim xmlDoc as freethreadeddomdoc30

    set xmldoc = new freethreadeddomdoc30
    xmldoc.loadxml xml
    xmldoc.selectnodes(".//Element/Value[position()=3]")

    I've tried every possible syntax that I can think of to accomplish this, and can't get it right. Anyone know how to go about doing this?

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: XML SelectNodes (Position)

    I'm not sure if you can do this with a single xpath. If you find a solution, please let me know! But it would seem that you first have to get each element and then select its third value.

    I think it depends on the order of operation. If the data were returned or filtered as some kind of matrix, where it kept an array of Elements and each array of Elements had its own Value array, then I could see doing something like what you're trying. But as far as I know, it returns your data as a single list of nodes.
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

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

Featured