本文討論如何使用 XML 路徑語言 (XPath) 表示式和 XPathNavigator 類別來查詢XPathDocument物件。
XPath 會以程式設計方式用來評估表達式,並選取檔中的特定節點。
本文參考Microsoft .NET Framework 類別庫命名空間 。 System.Xml.XPath
適用於: Visual Studio、.NET Framework
原始 KB 編號: 308333
先決條件
本文假設您已熟悉下列主題:
- Visual C#
- XML 術語
- 建立和讀取 XML 檔案
- XPath 語法
查詢包含 XPath 表達式的 XPathDocument
在 Microsoft Visual Studio 中,建立 Visual C# 控制台應用程式。
注意
此範例會使用名為 Books.xml的檔案。 您可以建立自己的 Books.xml 檔案,或使用 .NET 軟體開發工具包 (SDK) 快速入門隨附的範例。
如果您沒有安裝快速入門,而且不想安裝快速入門,請參閱 Books.xml 下載位置的<
相關內容>一節。 如果您已安裝快速入門,您可以在資料夾中找到
Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transforxml\VBBooks.xml 檔案。 您可以將檔案\Bin\Debug複製到您建立此項目之資料夾的子資料夾。請確定項目參考
System.Xml命名空間。using在和Xml命名空間上使用XPath語句,因此您不需要在程式代碼稍後限定這些命名空間中的宣告。 您可以在任何其他宣告之前使用using語句,如下所示:using System.Xml; using System.Xml.XPath;宣告適當的變數。
XPathDocument宣告物件來保存 XML 檔、XpathNavigator評估 XPath 表達式的物件,以及要逐一XPathNodeIterator查看所選節點的物件。String宣告物件以保存 XPath 表達式。 將宣告代碼新增至Class1中的Main函式。XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; String strExpression;載入
XPathDocument以及範例檔案Books.xml。 類別XPathDocument使用可擴充樣式表單語言轉換 (XSLT) 來提供快速且效能導向的 XML 檔案處理快取。 它類似於 XML 檔物件模型(DOM),但已針對 XSLT 處理和數據XPath模型進行高度優化。// Open the XML. docNav = new XPathDocument(@"c:\books.xml");XPathNavigator從檔案建立 。 對象XPathNavigator用於只讀 XPath 查詢。 XPath 查詢可能會傳回產生的值或許多節點。// Create a navigator to query with XPath. nav = docNav.CreateNavigator();建立 XPath 運算式以尋找書籍的平均成本。 這個 XPath 運算式會傳回單一值。 如需 XPath 語法的完整詳細數據,請參閱<
參考>一節中的 XPath 語法 。// Find the average cost of a book. // This expression uses standard XPath syntax. strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";Evaluate使用物件的 方法XPathNavigator來評估 XPath 表達式。 方法Evaluate會傳回表達式的結果。// Use the Evaluate method to return the evaluated expression. Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression));建立 XPath 運算式,以尋找所有花費超過 10 美元的書籍。 這個 XPath 運算式只會從 XML 來源傳回 Title 節點。
// Find the title of the books that are greater then $10.00. strExpression = "/bookstore/book/title[../price>10.00]";XPathNodeIterator建立與XPathNavigator的Select方法一起選取的節點。XPathNodeIterator表示 XPath 節點集,並支援此節點集上的作業。// Select the node and place the results in an iterator. NodeIter = nav.Select(strExpression);若要透過選取的節點移動,請使用
XPathNodeIterator從 的 方法傳回的SelectXPathNavigator。 在此情況下,您可以使用MoveNext的XPathNodeIterator方法來逐一查看所有選取的節點。Console.WriteLine("List of expensive books:"); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { Console.WriteLine("Book Title: {0}", NodeIter.Current.Value); };使用
ReadLine方法在主控台顯示完成後新增暫停,以便更容易顯示先前步驟的結果。// Pause Console.ReadLine();建置並執行專案。
注意
結果會顯示在主控台視窗中。
疑難排解
當您測試程式代碼時,可能會收到下列例外狀況錯誤訊息:
An unhandled exception of type System.Xml.XmlException occurred in System.xml.dll
Additional information: System error.
此例外狀況發生在下列程式代碼列上:
docNav = new XPathDocument("c:\\books.xml");
錯誤是由無效的處理指令所造成。 例如,處理指令可能包含多餘的空格。 下列範例是無效的處理指令:
<?xml version='1.0' ?>
若要解決例外狀況,請使用下列其中一個解決方案:
更正無效的處理指令。 下列範例是有效的處理指令:
<?xml version='1.0'?>從Books.xml檔案中移除 XML 處理指令。