none
Needing to query each repeated node for a specific child node, always getting the first node value RRS feed

  • Question

  • Take for example the following prototype XML layout

    <records>
      <record>
        <valuea>foo1</valuea>
        <valueb>bar1</valueb>
        <valuec>bob1</valuec>
      </record>
      <record>
        <valuea>foo2</valuea>
        <valueb>bar2</valueb>
        <valuec>bob2</valuec>
      </record>
      <record>
        <valuea>foo3</valuea>
        <valueb>bar3</valueb>
        <valuec>bob3</valuec>
      </record>
    </records>

    So I am using a call to .SelectNodes("//records/record") and iterating through within a For Each within the returned list.

    The iteration loop count is correct.

    Then I make a call to .SelectSingleNode("//records/record/valueb")

    For some reason I always get the first record instance. The object the For Each has received as the iteration valuable contains the correct values, but somehow making the call .SelectSingleNode against it to obtain the specific value the program needs retrieves the always the first record's valueb. The new object the For Each returns seems to have knowledge it is actually part of a larger set of data... not the individual record from the record set.

    How can I correctly iterate through the record list and obtain each distinct record's valueb?

    I am coding in Access / VBA.

    TIA!


    Michael Lueck - Lueck Data Systems - http://www.lueckdatasystems.com/ - My Blog

    Wednesday, June 12, 2019 6:34 PM

Answers

  • Try a different path:

     

      For Each record As XmlNode In doc.SelectNodes("//records/record")

     

        Dim valueb = record.SelectSingleNode("valueb")

     

        . . .

     

      Next

     

    Adjust it for VBA.

    • Marked as answer by Michael Lueck Friday, June 14, 2019 2:01 AM
    Thursday, June 13, 2019 6:13 AM

All replies

  • Hi Michael,

    Please share your source code as a starting point.

    I wish you could use .Net Framework and LINQ to XML...

    Wednesday, June 12, 2019 10:09 PM
  • Try a different path:

     

      For Each record As XmlNode In doc.SelectNodes("//records/record")

     

        Dim valueb = record.SelectSingleNode("valueb")

     

        . . .

     

      Next

     

    Adjust it for VBA.

    • Marked as answer by Michael Lueck Friday, June 14, 2019 2:01 AM
    Thursday, June 13, 2019 6:13 AM
  • Yes, .SelectSingleNode("valueb") was indeed the needed adjustment. I left the rest of my program the same, NOW it moves on through the result set, no longer returns the first record the number of times as the record count of the result set.

    Bravo Viorel_!

    I am thankful,


    Michael Lueck - Lueck Data Systems - http://www.lueckdatasystems.com/ - My Blog

    Friday, June 14, 2019 2:01 AM