Skip to content

Latest commit

 

History

History
169 lines (120 loc) · 7.43 KB

extract-xml-data-using-xpathnavigator.md

File metadata and controls

169 lines (120 loc) · 7.43 KB
description title ms.date dev_langs ms.assetid
Learn more about: Extract XML Data Using XPathNavigator
Extract XML Data Using XPathNavigator
03/30/2017
csharp
vb
095b0987-ee4b-4595-a160-da1c956ad576

Extract XML Data Using XPathNavigator

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

Convert an XPathNavigator to a String

The xref:System.Xml.XPath.XPathNavigator.OuterXml%2A property of the xref:System.Xml.XPath.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 xref:System.Xml.XPath.XPathNavigator.InnerXml%2A 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 xref:System.Xml.XPath.XPathNavigator object as a xref:System.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 xref:System.Xml.XPath.XPathNavigator.ReadSubtree%2A method is used to stream the entire contents of an XML document or just a single node and its child nodes to an xref:System.Xml.XmlReader object.

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

The xref:System.Xml.XPath.XPathNavigator object's position is unchanged by the creation or movement of the xref:System.Xml.XmlReader object. The xref:System.Xml.XPath.XPathNavigator.ReadSubtree%2A method is only valid when positioned on an element or root node.

The following example shows how to get an xref:System.Xml.XmlReader object containing the entire XML document in an xref:System.Xml.XPath.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.

[!code-xmlXPathXMLExamples#1]

Converting an XPathNavigator to an XmlWriter

The xref:System.Xml.XPath.XPathNavigator.WriteSubtree%2A method is used to stream the entire contents of an XML document or just a single node and its child nodes to an xref:System.Xml.XmlWriter object.

The xref:System.Xml.XPath.XPathNavigator object's position is unchanged by the creation of the xref:System.Xml.XmlWriter object.

The following example shows how to get an xref:System.Xml.XmlWriter object containing the entire XML document in an xref:System.Xml.XPath.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