Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Xml.XPath
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
XPathNavigator Class

Provides a cursor model for navigating and editing XML data.

Namespace: System.Xml.XPath
Assembly: System.Xml (in system.xml.dll)

Visual Basic (Declaration)
Public MustInherit Class XPathNavigator
    Inherits XPathItem
    Implements ICloneable, IXPathNavigable, IXmlNamespaceResolver
Visual Basic (Usage)
Dim instance As XPathNavigator
C#
public abstract class XPathNavigator : XPathItem, ICloneable, IXPathNavigable, IXmlNamespaceResolver
C++
public ref class XPathNavigator abstract : public XPathItem, ICloneable, IXPathNavigable, IXmlNamespaceResolver
J#
public abstract class XPathNavigator extends XPathItem implements ICloneable, IXPathNavigable, 
    IXmlNamespaceResolver
JScript
public abstract class XPathNavigator extends XPathItem implements ICloneable, IXPathNavigable, 
    IXmlNamespaceResolver

The XPathNavigator class in the System.Xml.XPath namespace is an abstract class which defines a cursor model for navigating and editing XML information items as instances of the XQuery 1.0 and XPath 2.0 Data Model.

An XPathNavigator object is created from a class that implements the IXPathNavigable interface such as the XPathDocument and XmlDocument classes. XPathNavigator objects created by XPathDocument objects are read-only while XPathNavigator objects created by XmlDocument objects can be edited. An XPathNavigator object's read-only or editable status is determined using the CanEdit property of the XPathNavigator class.

For more information about processing XML data using the XPath data model, see the Process XML Data Using the XPath Data Model topic.

Security noteSecurity Note

Exceptions raised as a result of using the XPathNavigator class, such as the XPathException class, may contain sensitive information that should not be exposed in untrusted scenarios. Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

Notes to Inheritors When you inherit from the XPathNavigator class, you must override the following members:

System.Object
   System.Xml.XPath.XPathItem
    System.Xml.XPath.XPathNavigator
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Navigage an XMLDocument Using an XPathNavigator Object      MikeMcIntyre MVP   |   Edit   |  

    Visual Basic Example
    Navigate an XMLDocument Using an XPathNavigator object.

 


    Private Sub NavigateXMLDocumentUsingXPathNavigator(ByVal peopleXmlFilePath As String)
        ' Declare a variable named peopleXmlDocument of type XmlDocument.
        Dim peopleXmlDocument As XmlDocument
        ' Instantiate a new XmlDocument object assigning it to the 'peopleXmlDocument variable.
        peopleXmlDocument = New XmlDocument
        ' Call the peopleXmlDocument object's Load method
        ' passing it the file path to a .xml file that defines persons.
        ' (see sample people XML file provided with this example)
        peopleXmlDocument.Load(peopleXmlFilePath)
        ' Declare a variable named theXPathNavigator of type XPathNavigator.
        Dim theXPathNavigator As XPathNavigator
        ' Call the peopleXmlDocument object's CreateNavigator method
        ' assigning the resulting XPathNavigator object to the 'theXPathNavigator' variable.
        theXPathNavigator = peopleXmlDocument.CreateNavigator
        ' Declare a variable named theXPathExpression of type XPathExpression.
        Dim theXPathExpression As XPathExpression
        ' Call theXPathNavigator object's Compile method passing it a query string;
        ' assign the resulting XPathPression object to the 'theXPathExpression' variable.
        theXPathExpression = theXPathNavigator.Compile("//person")
        ' Declare a variable named nodes of type XPathNodeIterator.
        Dim nodes As XPathNodeIterator
        ' Call theXPathNavigator's Select method passing it theXPathExpression;
        ' assign the resulting XPathNodeIterator object to the 'nodes' variable.
        nodes = theXPathNavigator.Select(theXPathExpression)
        ' Iterate through the person nodes writing out each person's name,
        ' address type, and street address.
        While nodes.MoveNext
            ' Navigate to person's name element.
            nodes.Current.MoveToChild("name", "")
            Console.Write("Name: " & nodes.Current.Value.ToString & Environment.NewLine)
            ' Move back to person element.
            nodes.Current.MoveToParent()
            ' Navigate to person's address element.
            nodes.Current.MoveToChild("address", "")
            Console.Write("Address Type: " & nodes.Current.GetAttribute("addressType", "") & Environment.NewLine)
            ' Navigate to address element's street element.
            nodes.Current.MoveToChild("street", "")
            Console.Write("Street Address: " & nodes.Current.Value & Environment.NewLine & "-------------" & Environment.NewLine)
            ' Move back to person's address element.
            nodes.Current.MoveToParent()
            ' Move back to the person.
            nodes.Current.MoveToParent()
        End While
    End Sub
 

 

Data for Example - Place the XML below in a file named 'Persons.xml'

<?xml version="1.0" encoding="utf-8" ?>
<people>
 <person >
  <id>1</id>
  <name>James Wentnet</name>
  <address addressType="mail">
   <street>123 Elm Avenue</street>
   <city>San Francisco</city>
   <postal_code>95466</postal_code>
  </address>
 </person>
 <person >
  <id>1</id>
  <name>Robert Gotnet</name>
  <address addressType="delivery">
   <street>123 Elm Avenue</street>
   <city>San Francisco</city>
   <postal_code>95466</postal_code>
  </address>
 </person>
</people>

 

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker