使用 XPathNavigator 提取 XML 数据

可以通过多种不同的方式在 Microsoft .NET Framework 中表示 XML 文档。 包括使用 String,或通过使用 XmlReaderXmlWriterXmlDocumentXPathDocument 类。 为了便于在这些不同的 XML 文档表示形式之间切换,XPathNavigator 类提供了许多方法和属性,用于将 XML 作为 String, XmlReader 对象或 XmlWriter 对象提取。

将 XPathNavigator 转换为字符串

OuterXml 类的 XPathNavigator 属性用于获取整个 XML 文档的标记或只获取单个节点及其子节点的标记。

注意

InnerXml 属性只获取节点的子节点的标记。

以下代码示例显示如何将 XPathNavigator 对象中包含的整个 XML 文档以及单个节点及其子节点保存为 String

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;  

将 XPathNavigator 转换为 XmlReader

ReadSubtree 方法用于将 XML 文档的全部内容或只是单个节点及其子节点流处理到 XmlReader 对象。

为当前节点及其子节点创建 XmlReader 对象时,XmlReader 对象的 ReadState 属性设置为 Initial。 当首次调用 XmlReader 对象的 Read 方法时,XmlReader 移动到 XPathNavigator 的当前节点上。 新的 XmlReader 对象继续执行读取操作,直到到达 XML 树的结尾为止。 此时,Read 方法返回 falseXmlReader 对象的 ReadState 属性设置为 EndOfFile

创建或移动 XPathNavigator 对象时不会更改 XmlReader 对象的位置。 ReadSubtree 方法只有在位于元素或根节点上时才有效。

以下示例显示如何获取包含 XmlReader 对象中的整个 XML 文档以及单个节点及其子节点的 XPathDocument 对象。

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();  

该示例使用 books.xml 文件作为输入。

<?xml version="1.0" encoding="utf-8" ?> 
<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>

将 XPathNavigator 转换为 XmlWriter

WriteSubtree 方法用于将 XML 文档的全部内容或只是单个节点及其子节点流处理到 XmlWriter 对象。

创建 XPathNavigator 对象时不会更改 XmlWriter 对象的位置。

以下示例显示如何获取包含 XmlWriter 对象中的整个 XML 文档以及单个节点及其子节点的 XPathDocument 对象。

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();  

该示例使用本主题前面所示的 books.xml 文件作为输入。

请参阅