在 Microsoft .NET Framework 中,有數種不同的表示 XML 檔的方式。 這包括使用String,或使用XmlReader、XmlWriter、XmlDocument或XPathDocument類別。 為了方便在 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 方法會傳 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
本主題稍早找到的檔案做為輸入。