Share via


Extract XML Data using XPathNavigator 

There are several different ways to represent an XML document in the Microsoft .NET Framework. This includes using a String, or by using the XmlReader, XmlWriter, XmlDocument, or XPathDocument classes. To facilitate moving between these different representations of an XML document, the XPathNavigator class provides a number of methods and properties for extracting the XML as a String, XmlReader object or XmlWriter object.

Convert an XPathNavigator to a String

The OuterXml property of the XPathNavigator class is used to get the markup of the entire XML document or just the markup of a single node and its child nodes.

Note

The InnerXml property gets the markup of just the child nodes of a node.

The following code example shows how to save an entire XML document contained in an XPathNavigator object as a String, as well as a single node and its child nodes.

Dim document As XPathDocument = New XPathDocument("input.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Save the entire input.xml document to a string.
Dim xml As String = navigator.OuterXml

' Now save the Root element and its child nodes to a string.
navigator.MoveToChild(XPathNodeType.Element)
Dim root As String = navigator.OuterXml
XPathDocument document = new XPathDocument("input.xml");
XPathNavigator navigator = document.CreateNavigator();

// Save the entire input.xml document to a string.
string xml = navigator.OuterXml;

// Now save the Root element and its child nodes to a string.
navigator.MoveToChild(XPathNodeType.Element);
string root = navigator.OuterXml;

Convert an XPathNavigator to an XmlReader

The ReadSubtree method is used to stream the entire contents of an XML document or just a single node and its child nodes to an XmlReader object.

When the XmlReader object is created with the current node and its child nodes, the XmlReader object's ReadState property is set to Initial. When the XmlReader object's Read method is called for the first time, the XmlReader is moved to the current node of the XPathNavigator. The new XmlReader object continues to read until the end of the XML tree is reached. At this point, the Read method returns false and the XmlReader object's ReadState property is set to EndOfFile.

The XPathNavigator object's position is unchanged by the creation or movement of the XmlReader object. The ReadSubtree method is only valid when positioned on an element or root node.

The following example shows how to get an XmlReader object containing the entire XML document in an XPathDocument object as well as a single node and its child nodes.

Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Stream the entire XML document to the XmlReader.
Dim xml As XmlReader = navigator.ReadSubtree()

While xml.Read()
    Console.WriteLine(xml.ReadInnerXml())
End While

xml.Close()

' Stream the book element and its child nodes to the XmlReader.
navigator.MoveToChild("bookstore", "")
navigator.MoveToChild("book", "")

Dim book As XmlReader = navigator.ReadSubtree()

While book.Read()
    Console.WriteLine(book.ReadInnerXml())
End While

book.Close()
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Stream the entire XML document to the XmlReader.
XmlReader xml = navigator.ReadSubtree();

while (xml.Read())
{
    Console.WriteLine(xml.ReadInnerXml());
}

xml.Close();

// Stream the book element and its child nodes to the XmlReader.
navigator.MoveToChild("bookstore", "");
navigator.MoveToChild("book", "");

XmlReader book = navigator.ReadSubtree();

while (book.Read())
{
    Console.WriteLine(book.ReadInnerXml());
}

book.Close();

The example takes the books.xml file as input.

<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Converting an XPathNavigator to an XmlWriter

The WriteSubtree method is used to stream the entire contents of an XML document or just a single node and its child nodes to an XmlWriter object.

The XPathNavigator object's position is unchanged by the creation of the XmlWriter object.

The following example shows how to get an XmlWriter object containing the entire XML document in an XPathDocument object as well as a single node and its child nodes.

Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Stream the entire XML document to the XmlWriter.
Dim xml As XmlWriter = XmlWriter.Create("newbooks.xml")
navigator.WriteSubtree(xml)
xml.Close()

' Stream the book element and its child nodes to the XmlWriter.
navigator.MoveToChild("bookstore", "")
navigator.MoveToChild("book", "")

Dim book As XmlWriter = XmlWriter.Create("book.xml")
navigator.WriteSubtree(book)
book.Close()
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Stream the entire XML document to the XmlWriter.
XmlWriter xml = XmlWriter.Create("newbooks.xml");
navigator.WriteSubtree(xml);
xml.Close();

// Stream the book element and its child nodes to the XmlWriter.
navigator.MoveToChild("bookstore", "");
navigator.MoveToChild("book", "");

XmlWriter book = XmlWriter.Create("book.xml");
navigator.WriteSubtree(book);
book.Close();

The example takes the books.xml file found earlier in this topic as input.

See Also

Reference

XmlDocument
XPathDocument
XPathNavigator

Concepts

Process XML Data Using the XPath Data Model
Node Set Navigation Using XPathNavigator
Attribute and Namespace Node Navigation Using XPathNavigator
Accessing Strongly Typed XML Data using XPathNavigator