XPathNavigator Class
Namespace: System.Xml.XPath
Represents a node in an XML document or stream, and provides methods for navigating to related nodes.
Overview
The XPathNavigator class provides a read-only, forward-only, non-cached random access interface to an XML document. It allows you to navigate through the document, select nodes, and retrieve their values. This class is fundamental for implementing XPath queries and XSLT transformations.
XPathNavigator is designed to be lightweight and efficient, making it suitable for processing large XML documents. It supports various node types, including elements, attributes, text nodes, comments, and processing instructions.
Constructors
The XPathNavigator class itself is abstract and cannot be instantiated directly. You typically obtain an instance of a concrete XPathNavigator implementation (such as XPathDocument.CreateNavigator()) from an XML document or data source.
Properties
- BaseURI: Gets the base URI of the current node.
- IsDefaultAttributeNode: Gets a value indicating whether the current node is an attribute node that was defaulted by the parser.
- LocalName: Gets the qualified name of the current node, excluding the namespace prefix.
- LookupNamespace: Gets the namespace URI for the specified prefix.
- Name: Gets the qualified name of the current node.
- NamespaceURI: Gets the namespace URI of the current node.
- NodeType: Gets the type of the current node.
- Prefix: Gets the namespace prefix of the current node.
- Value: Gets the value of the current node.
Methods
-
Clone()
public virtual XPathNavigator Clone()
Creates a duplicate of the
XPathNavigatorobject. -
CompareTo(XPathNavigator other)
public virtual int CompareTo(XPathNavigator other)
Compares the current
XPathNavigatorwith anotherXPathNavigator. -
CreateNavigator()
public virtual XPathNavigator CreateNavigator()
Creates a new
XPathNavigatorthat can navigate the current node. -
GetAttribute(string localName, string namespaceURI)
public virtual string GetAttribute(string localName, string namespaceURI)
Gets the value of the attribute with the specified local name and namespace URI.
-
IsDescendant(XPathNavigator other)
public virtual bool IsDescendant(XPathNavigator other)
Determines whether the specified
XPathNavigatoris a descendant of the current node. -
MoveTo(XPathNavigator other)
public virtual bool MoveTo(XPathNavigator other)
Moves the
XPathNavigatorto the same node as the specifiedXPathNavigator. -
MoveToAttribute(string localName, string namespaceURI)
public virtual bool MoveToAttribute(string localName, string namespaceURI)
Moves the
XPathNavigatorto the attribute with the specified local name and namespace URI. -
MoveToAttribute(int i)
public virtual bool MoveToAttribute(int i)
Moves the
XPathNavigatorto the attribute at the specified index. -
MoveToChild(string localName, string namespaceURI)
public virtual bool MoveToChild(string localName, string namespaceURI)
Moves the
XPathNavigatorto the child element with the specified local name and namespace URI. -
MoveToChild(int i)
public virtual bool MoveToChild(int i)
Moves the
XPathNavigatorto the child node at the specified index. -
MoveToChildren()
public virtual bool MoveToChildren()
Moves the
XPathNavigatorto the first child node. -
MoveToDocumentRoot()
public virtual bool MoveToDocumentRoot()
Moves the
XPathNavigatorto the document root node. -
MoveToFirst()
public virtual bool MoveToFirst()
Moves the
XPathNavigatorto the first sibling node. -
MoveToFirstAttribute()
public virtual bool MoveToFirstAttribute()
Moves the
XPathNavigatorto the first attribute node. -
MoveToId(string id)
public virtual bool MoveToId(string id)
Moves the
XPathNavigatorto the node with the specified ID. -
MoveToNext()
public virtual bool MoveToNext()
Moves the
XPathNavigatorto the next sibling node. -
MoveToNextAttribute()
public virtual bool MoveToNextAttribute()
Moves the
XPathNavigatorto the next attribute node. -
MoveToParent()
public virtual bool MoveToParent()
Moves the
XPathNavigatorto the parent node. -
MoveToPrevious()
public virtual bool MoveToPrevious()
Moves the
XPathNavigatorto the previous sibling node. -
MoveToRoot()
public virtual bool MoveToRoot()
Moves the
XPathNavigatorto the document root. -
MoveToAttribute(string name)
public virtual bool MoveToAttribute(string name)
Moves the
XPathNavigatorto the attribute with the specified name. -
MoveToAttribute(string localName, string namespaceURI)
public virtual bool MoveToAttribute(string localName, string namespaceURI)
Moves the
XPathNavigatorto the attribute with the specified local name and namespace URI.
Remarks
XPathNavigator is an abstract base class. To use it, you typically create an instance of a class that implements it, such as:
System.Xml.XPath.XPathDocument: An efficient, read-only, in-memory XML document.System.Xml.XmlDocument: The standard DOM implementation for XML.
You can obtain a navigator from these classes using their CreateNavigator() method.
using System;
using System.Xml;
using System.Xml.XPath;
public class Example
{
public static void Main(string[] args)
{
string xmlString = @"
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XPathNavigator navigator = doc.CreateNavigator();
// Move to the first book element
if (navigator.MoveToChild("bookstore", ""))
{
if (navigator.MoveToChild("book", ""))
{
// Get the category attribute
string category = navigator.GetAttribute("category", "");
Console.WriteLine($"Category: {category}");
// Move to the title element
if (navigator.MoveToChild("title", ""))
{
Console.WriteLine($"Title: {navigator.Value}");
navigator.MoveToParent(); // Back to book
}
navigator.MoveToParent(); // Back to bookstore
}
navigator.MoveToParent(); // Back to root
}
}
}
Requirements
Namespace: System.Xml.XPath
Assembly: System.Xml (in System.Xml.dll)
Platforms: .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5, .NET Framework 4.0, .NET Framework 4.5, .NET Framework 4.6, .NET Framework 4.7, .NET Framework 4.8
Security: Full trust for the immediate caller. This member cannot be used by code that is executing in a partially trusted context.