XML navigation in VB .NET

Posted on 2009-05-04
Last Modified: 2012-05-06
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"?>




    <field2>This is the text I need for 0001</field2>




    <field2>This is the text I need for 0002</field2>





  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

Question by:jes12345
  • 3

Accepted Solution

mvgeertruyen earned 500 total points
ID: 24295649
Not perfect but answers your question:
        xmlNI = xmlNav.Select("/message/value")

        While (xmlNI.MoveNext())

            If xmlNI.Current.SelectSingleNode("field1").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


Author Comment

ID: 24297265
Thanks for the advise - I have left the office for the day but will test it out first thing.
Thanks J

Author Comment

ID: 24302411
Woked like a charm - many thanks for the advise! J

Author Closing Comment

ID: 31577524
I think it was perfect:)

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now