XPathNavigator.ReadSubtree 方法

定義

傳回 XmlReader 物件,此物件包含目前的節點和它的子節點。

public:
 virtual System::Xml::XmlReader ^ ReadSubtree();
public virtual System.Xml.XmlReader ReadSubtree ();
abstract member ReadSubtree : unit -> System.Xml.XmlReader
override this.ReadSubtree : unit -> System.Xml.XmlReader
Public Overridable Function ReadSubtree () As XmlReader

傳回

XmlReader

XmlReader 物件,其包含目前的節點和它的子節點。

例外狀況

XPathNavigator 不是位於項目節點或根節點上。

範例

下列範例說明如何在 ReadSubtree 檔案的第一個專案 book contosoBooks.xml 上使用 方法。

XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

XmlReader^ reader = navigator->ReadSubtree();

while (reader->Read())
{
    Console::WriteLine(reader->ReadInnerXml());
}

reader->Close();
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

XmlReader reader = navigator.ReadSubtree();

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

reader.Close();
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

Dim reader As XmlReader = navigator.ReadSubtree()

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

reader.Close()

範例將 contosoBooks.xml 檔案做為輸入。

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <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>  

備註

以目前節點及其子節點建立 XmlReader 物件時,會將 XmlReader 物件的 ReadState 屬性設為 Initial。 當初次呼叫 XmlReader 物件的 Read 方法時,XmlReader 會移至 XPathNavigator 的目前節點。 新的 XmlReader 物件在到達 XML 樹狀目錄的尾端之前,會持續讀取。 此時,Read 方法會傳回 false,且 XmlReader 物件的 ReadState 屬性會設為 EndOfFile

注意

變更物件的位置 XmlReader 不會影響 的位置 XPathNavigator

目前節點之範圍內命名空間的命名空間宣告不會插入提供給 XmlReader 物件的 XML 資料流程中。

注意

此行為與 WriteSubtree 方法不同。

這個方法會 XmlReader 建立具有特定讀取器設定的物件,而使用此方法的模組無法控制這些設定。 例如,此方法所傳回的讀取器會禁止 (DTD) 處理資料類型定義。 如果讀取器嘗試讀取使用 DTD 的檔案,它會擲回錯誤 。 XmlException 例外狀況的訊息將是 Unexpected DTD declaration

您可以實作傳回 XmlReader 具有所需 XmlReaderSettings 之 的自訂 XmlResolver ,以變更此行為。

適用於