How to navigate XML with the XPathNavigator class by using Visual Basic

Article translations Article translations
Article ID: 301111 - View products that this article applies to.
This article was previously published under Q301111
For a Microsoft Visual C# version of this article, see 308343.
For a Microsoft Visual Basic 6.0 version of this article, see 271619.
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article illustrates how to navigate Extensible Markup Language (XML) documents with an XPathNavigator object that is created from an XPathDocument object. This sample loads an XPathDocument object with XML data, creates an XPathNavigator object as a view onto the data, and displays the XML by walking through the document.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
  • Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • XML terminology
  • Creating and reading XML files
  • XML Path Language (XPath) syntax

How to use the XPathNavigator class to navigate XML

  1. In Visual Studio 2005 or in Visual Studio. NET, create a Visual Basic 2005 or Visual Basic .NET Console Application.

    Note This example uses a file that is named Books.xml. You can create your own Books.xml file, or you can use the sample that is included with the .NET Software Development Kit (SDK) Quickstarts. If you have the Quickstarts installed, Books.xml is located in the following folder:
    \Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transformxml\VB
    Alternatively, you can obtain this file by visiting the following Web site:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx

    You must copy Books.xml to the \Bin\Debug folder that is located under the folder in which you created this project.
  2. Make sure that the project references the System.Xml namespace.
  3. Use the Imports statement on the Xml and XPath namespaces so that you are not required to qualify declarations in those namespaces later in your code. You must use the Imports statement prior to any other declarations.
    Imports System.Xml
    Imports System.Xml.XPath
    					
  4. Declare the appropriate variables. Declare an XPathDocument object to hold the XML document and an XPathNavigator object to evaluate XPath expressions and move through the document. Declare a String object to hold the XPath expression. Add the declaration code in the Main procedure in Module1.
    Dim nav As XPathNavigator
    Dim docNav As XPathDocument
    					
  5. Load an XPathDocument object with the sample file Books.xml. The XPathDocument class uses Extensible Stylesheet Language Transformations (XSLT) to provide a fast and performance-oriented cache for XML document processing. It is similar to the XML Document Object Model (DOM) but is highly optimized for XSLT processing and the XPath data model.
    'Open the XML.
    docNav = New XPathDocument("books.xml")
    					
  6. Create an XPathNavigator object from the document. XPathNavigator enables you to move through both the attributes nodes and the namespace nodes in an XML document.
    'Create a navigator to query with XPath.
    nav = docNav.CreateNavigator
    					
  7. Move to the root of the document with the MoveToRoot method. MoveToRoot sets the navigator to the document node that contains the entire tree of nodes.
    'Initial XPathNavigator to start at the root.
    nav.MoveToRoot()
    					
  8. Use the MoveToFirstChild method to move to the children of the XML document. The MoveToFirstChild method moves to the first child of the current node. In the case of the Books.xml source, you are moving away from the root document into the children, the Comment section, and the Bookstore node.
    'Move to the first child node (comment field).
    nav.MoveToFirstChild()
    					
  9. Use the MoveToNext method to iterate through nodes at the sibling level. The MoveToNext method moves to the next sibling of the current node.
    'Loop through all the root nodes.
    Do
        ...
    Loop While nav.MoveToNext
    					
  10. Use the NodeType property to make sure that you are only processing element nodes, and use the Value property to display the text representation of the element.
    Do
        'Find the first element.
        If nav.NodeType = XPathNodeType.Element Then
    'If children exist.
    If nav.HasChildren Then
    
        'Move to the first child.
        nav.MoveToFirstChild()
    
        'Loop through all the children.
        Do
    'Display the data.
    Console.Write("The XML string for this child ")
    Console.WriteLine("is '{0}'", nav.Value)
    Loop While nav.MoveToNext
    
    End If
        End If
    Loop While nav.MoveToNext
    					
  11. Use the HasAttributes property to determine whether a node has any attributes. You can also use other methods, such as MoveToNextAttribute, to move to an attribute and inspect its value. Note that this code segment only walks through the descendents of the root node and not the entire tree.
    Do
        'Find the first element.
        If nav.NodeType = XPathNodeType.Element Then
    'if children exist
    If nav.HasChildren Then
    
        'Move to the first child.
        nav.MoveToFirstChild()
    
        'Loop through all the children.
        Do
    'Display the data.
    Console.Write("The XML string for this child ")
    Console.WriteLine("is '{0}'", nav.Value)
    
    'Check for attributes.
    If nav.HasAttributes Then
        		Console.WriteLine("This node has attributes")
    End If
    Loop While nav.MoveToNext
    
    End If
        End If
    Loop While nav.MoveToNext
    					
  12. Use the ReadLine method of the Console object to add a pause at the end of the console display to more readily display the above results.
    'Pause.
    Console.ReadLine()
    					
  13. Build and run your Console Application project.

Complete code listing

Imports System.Xml
Imports System.Xml.XPath

Module Module1

    Sub Main()
        Dim nav As XPathNavigator
        Dim docNav As XPathDocument
        docNav = New XPathDocument("books.xml")
        nav = docNav.CreateNavigator
        nav.MoveToRoot()
        'Move to the first child node (comment field).
        nav.MoveToFirstChild()

        Do
            'Find the first element.
            If nav.NodeType = XPathNodeType.Element Then
                'if children exist
                If nav.HasChildren Then

                    'Move to the first child.
                    nav.MoveToFirstChild()

                    'Loop through all the children.
                    Do
                        'Display the data.
                        Console.Write("The XML string for this child ")
                        Console.WriteLine("is '{0}'", nav.Value)

                        'Check for attributes.
                        If nav.HasAttributes Then
                            Console.WriteLine("This node has attributes")
                        End If
                    Loop While nav.MoveToNext

                End If
            End If
        Loop While nav.MoveToNext

        'Pause.
        Console.ReadLine()

    End Sub

End Module
				

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
XML in .NET: .NET Framework XML classes and C# offer simple, scalable data manipulation
http://msdn.microsoft.com/msdnmag/issues/01/01/xml

XPathNavigator class
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.aspx

XPathDocument class
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathdocument.aspx

XSLT transformations with the XslTransform class
http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltransform.aspx

XPath examples
http://msdn.microsoft.com/en-us/library/ms256086.aspx
For more information, visit the following World Wide Web Consortium (W3C) Web site:
XML Path Language (XPath)
Version 1.0: W3C Recommendation 16 November 1999
http://www.w3.org/TR/1999/REC-xpath-19991116

Properties

Article ID: 301111 - Last Review: July 5, 2012 - Revision: 9.0
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005applies kbvs2005swept kbdownload kbsample kbhowtomaster KB301111

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com