Link to home
Start Free TrialLog in
Avatar of jes12345
jes12345

asked on

XML navigation in VB .NET

I am new to xml navigation in .NET so I have a fairly basic question I hope someone can help me with.

I have a xml file with a similar structure as shown below. I iterate through the node list and find the node I need based on field1. However I am not sure how I can reach the value I need which is field2.

Maybe it is a better approach to set the navigator to ""/message/value" and then retreive each child individually. If so any advise of the syntax will be very good.

Thanks J
<?xml version="1.0" encoding="iso-8859-1"?>
<message>
  <value>
    <field1>0001</field1>
    <field2>This is the text I need for 0001</field2>
  </value>
  <value>
    <field1>0002</field1>
    <field2>This is the text I need for 0002</field2>
  </value>
</message>
 
 
  Dim xpathDoc As XPathDocument
        Dim xmlNav As XPathNavigator
        Dim xmlNI As XPathNodeIterator
 
        Dim _strValue As String = ""
 
 
        xpathDoc = New XPathDocument("C:\test.xml")
        xmlNav = xpathDoc.CreateNavigator()
 
 
       xmlNI = xmlNav.Select("/message/value/field1")
 
            While (xmlNI.MoveNext())
 
                If xmlNI.Current.Value = "0001" Then
                    _strValue= some code here to retrieve value "This is the text I need 0001"
                    Exit While
                End If
 
            End While

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mvgeertruyen
mvgeertruyen
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jes12345
jes12345

ASKER

Thanks for the advise - I have left the office for the day but will test it out first thing.
Thanks J
Woked like a charm - many thanks for the advise! J
I think it was perfect:)