通过


使用 XPathNavigator 提取 XML 数据

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

将 XPathNavigator 转换为字符串

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

注释

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

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

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对象时,该对象的XmlReaderReadState属性设置为Initial。 首次调用XmlReader对象的Read方法时,XmlReader将移动到XPathNavigator的当前节点。 新 XmlReader 对象继续读取,直到到达 XML 树的末尾。 此时,该方法 Read 返回 false ,并且 XmlReader 对象的 ReadState 属性设置为 EndOfFile

XPathNavigator对象的位置不受XmlReader对象的创建或移动影响。 ReadSubtree仅当定位在元素或根节点上时,该方法才有效。

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

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对象,其中XPathDocument对象包含整个 XML 文档,以及单个节点及其子节点。

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文件作为输入。

另请参阅