vb.netforeachselectnodes

Getting position of the XML element inside For Each loop


How do I get the position of the XML element in this loop?

For Each objNode In objDoc.SelectNodes("//books/book")

 ???

Next

What I want in output would be something like

1 2 3 4 ....


Solution

  • You probably want something like:

    objBooks = objDoc.SelectSingleNode("//books")
    
    Dim pos As Integer = 1
    For Each book As XmlNode In objBooks.ChildNodes
    
       Console.Write(pos & " ")
    
       pos = pos + 1
    Next