XPathDocument Class

Namespace: System.Xml.XPath

Assembly: System.Xml.dll

Overview

Represents an immutable, read-only, in-memory XPath data model instance. The XPathDocument class provides a fast, forward-only, read-only XML data store that can be used for XPath queries. It is optimized for performance when you are querying XML data.

XPathDocument is a read-only representation of an XML document. If you need to modify the XML document, use the XmlDocument class. XPathDocument is an efficient way to query large XML documents.

Syntax

public sealed class XPathDocument

Constructors

Name Description
XPathDocument() Initializes a new instance of the XPathDocument class.
XPathDocument(Stream stream) Initializes a new instance of the XPathDocument class using the specified stream.
XPathDocument(string uri) Initializes a new instance of the XPathDocument class using the specified URI.
XPathDocument(XmlReader reader) Initializes a new instance of the XPathDocument class using the specified XmlReader.
XPathDocument(string text, XmlSpace space) Initializes a new instance of the XPathDocument class using the specified text and XmlSpace.
XPathDocument(string uri, XmlType type) Initializes a new instance of the XPathDocument class using the specified URI and XmlType.

Properties

Name Description
BaseURI Gets the base URI of the current node.
DocumentType Gets a XmlDocumentType object representing the DTD of the document.
Document{Xml} Gets an XmlDocument object that represents the entire XML document.

Methods

Name Description
CreateNavigator() Creates a new XPathNavigator object that can navigate the document.
Load(Stream stream) Loads the XML document from the specified stream.
Load(string uri) Loads the XML document from the specified URI.

Remarks

The XPathDocument class implements the IXsltContext interface, which allows it to be used with the XslTransform class for XSLT transformations. It is a memory-efficient representation of XML data, designed for scenarios where read-only access and fast querying are paramount.

When you load an XML document into an XPathDocument, it is converted into an internal, optimized representation that is suitable for XPath data model traversal. This process is typically faster than loading into an XmlDocument, especially for large documents.

The CreateNavigator() method is the primary way to interact with the data within an XPathDocument. The returned XPathNavigator object can be used to select nodes, evaluate XPath expressions, and retrieve node values.

Example

The following example demonstrates how to load an XML document into an XPathDocument and query it using an XPathNavigator.

using System;
using System.Xml;
using System.Xml.XPath;

public class Example
{
    public static void Main(string[] args)
    {
        // Sample XML string
        string xmlString = @@"<books>
            <book id='bk101'>
                <author>Gambardella, Matthew</author>
                <title>XML Developer's Guide</title>
                <genre>Computer</genre>
                <price>44.95</price>
            </book>
            <book id='bk102'>
                <author>Ralls, Kim</author>
                <title>Midnight Rain</title>
                <genre>Fantasy</genre>
                <price>5.95</price>
            </book>
        </books>";

        // Load the XML string into an XPathDocument
        using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
        {
            XPathDocument doc = new XPathDocument(reader);

            // Create an XPathNavigator to query the document
            XPathNavigator navigator = doc.CreateNavigator();

            // Select all book titles
            XPathNodeIterator nodes = navigator.Select("/books/book/title/text()");

            Console.WriteLine("Book Titles:");
            while (nodes.MoveNext())
            {
                Console.WriteLine($"- {nodes.Current.Value}");
            }

            // Select books with a price greater than 10
            nodes = navigator.Select("/books/book[price > 10]");

            Console.WriteLine("\nBooks with price > 10:");
            while (nodes.MoveNext())
            {
                Console.WriteLine($"- Title: {nodes.Current.SelectSingleNode("title").Value}, Price: {nodes.Current.SelectSingleNode("price").Value}");
            }
        }
    }
}

Requirements

Component Version
.NET Framework 1.0 in the .NET Framework
.NET Core 2.0
.NET Standard 2.0
Windows Windows XP SP2, Windows Server 2003 SP1, or later
Header #include <system/xml/xpath.h>
Namespace System.Xml.XPath
Assembly System.Xml.dll