共用方式為


使用 XPathNavigator 擷取 XML 資料

更新: November 2007

可採用幾種不同方式表示 Microsoft .NET Framework 中的 XML 文件。包括使用 String,或使用 XmlReaderXmlWriterXmlDocumentXPathDocument 類別。為便於在 XML 文件的不同表示之間進行切換,XPathNavigator 類別提供了一些方法及屬性,可將 XML 當做 StringXmlReader 物件或 XmlWriter 物件擷取。

將 XPathNavigator 轉換為字串

可使用 XPathNavigator 類別的 OuterXml 屬性,取得整個 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 方法會傳回 false,且 XmlReader 物件的 ReadState 屬性會設為 EndOfFile

建立或移動 XmlReader 物件,並不會變更 XPathNavigator 物件的位置。僅當定位在項目或根節點上時,ReadSubtree 方法才有效。

下列範例顯示如何取得包含 XPathDocument 物件中整個 XML 文件與單一節點及其子節點的 XmlReader 物件。

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 檔案做為輸入。

<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 物件。

建立 XmlWriter 物件,並不會變更 XPathNavigator 物件的位置。

下列範例顯示如何取得包含 XPathDocument 物件中整個 XML 文件與單一節點及其子節點的 XmlWriter 物件。

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 檔案當做輸入。

請參閱

概念

使用 XPath 資料模型處理 XML 資料

使用 XPathNavigator 的節點集巡覽

使用 XPathNavigator 的屬性及命名空間節點巡覽

使用 XPathNavigator 存取強型別 XML 資料

參考

XmlDocument

XPathDocument

XPathNavigator