小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

HOW TO:使用 Visual C# .NET 通過(guò) XPath 表達(dá)式查詢 XML

 素行 2007-01-06

HOW TO:使用 Visual C# .NET 通過(guò) XPath 表達(dá)式查詢 XML

文章編號(hào) : 308333
最后修改 : 2006年4月28日
修訂 : 2.0
本文的發(fā)布號(hào)曾為 CHS308333

概要

本文演示如何使用 XPathNavigator 類通過(guò) XML 路徑語(yǔ)言 (XPath) 表達(dá)式查詢 XPathDocument 對(duì)象。XPath 用于以編程方式計(jì)算表達(dá)式并選擇文檔中的特定節(jié)點(diǎn)。

要求

下面的列表列出了推薦使用的硬件、軟件、網(wǎng)絡(luò)基礎(chǔ)結(jié)構(gòu)以及所需的服務(wù)包:
Visual C# .NET
本文假定您熟悉下列主題:
XML 術(shù)語(yǔ)
創(chuàng)建和讀取 XML 文件
XPath 語(yǔ)法

如何用 XPath 表達(dá)式查詢 XML

1. 在 Visual Studio .NET 中新建一個(gè) Visual C# .NET 控制臺(tái)應(yīng)用程序。

備注:本示例使用名為 Books.xml 的文件。您可以創(chuàng)建自己的 Books.xml 文件,也可以使用 .NET 軟件開(kāi)發(fā)工具包 (SDK) 快速入門中包括的示例。如果您沒(méi)有安裝"快速入門"而且也不想安裝它們,請(qǐng)參閱 Books.xml 下載位置的"參考"部分。如果已經(jīng)安裝了"快速入門",則該文件位于以下文件夾中:
Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transformxml\VB
必須將該文件復(fù)制到 \Bin\Debug 文件夾,該文件夾位于您在其中創(chuàng)建該項(xiàng)目的文件夾中。
2. 確保該項(xiàng)目引用 System.Xml 名稱空間。
3. Xml XPath 名稱空間上使用 using 語(yǔ)句,這樣以后就不需要在代碼中限定這些名稱空間中的聲明了。using 語(yǔ)句必須在所有其他聲明之前使用,如下所示:
using System.Xml;
            using System.Xml.XPath;
4. 聲明合適的變量。聲明 XPathDocument 對(duì)象以保存 XML 文檔,聲明 XpathNavigator 對(duì)象以計(jì)算 XPath 表達(dá)式,聲明 XPathNodeIterator 對(duì)象以迭代通過(guò)選定節(jié)點(diǎn)。聲明 String 對(duì)象以保存 XPath 表達(dá)式。在 Class1 的 Main 函數(shù)中添加聲明代碼。
XPathNavigator nav;
            XPathDocument docNav;
            XPathNodeIterator NodeIter;
            String strExpression;
5. 用示例文件 Books.xml 加載 XPathDocument。XPathDocument 類使用可擴(kuò)展樣式表語(yǔ)言轉(zhuǎn)換 (XSLT) 為 XML 文檔處理提供快速和面向性能的緩存。它類似于 XML 文檔對(duì)象模型 (DOM),但經(jīng)過(guò)了高度優(yōu)化,以用于 XSLT 處理和 XPath 數(shù)據(jù)模型。
// Open the XML.
            docNav = new XPathDocument(@"c:\books.xml");
6. 從文檔創(chuàng)建 XPathNavigatorXPathNavigator 對(duì)象用于進(jìn)行只讀 XPath 查詢。XPath 查詢可返回結(jié)果值或許多節(jié)點(diǎn)。
// Create a navigator to query with XPath.
            nav = docNav.CreateNavigator();
7. 創(chuàng)建 XPath 表達(dá)式以查找圖書(shū)的平均價(jià)格。這個(gè) XPath 表達(dá)式返回單個(gè)值。有關(guān) XPath 語(yǔ)法的完整詳細(xì)信息,請(qǐng)參見(jiàn)"參考"部分中的"XPath 語(yǔ)法"。
// Find the average cost of a book.
            // This expression uses standard XPath syntax.
            strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";
8. 使用 XPathNavigator 對(duì)象的 Evaluate 方法計(jì)算 XPath 表達(dá)式。Evaluate 方法返回該表達(dá)式的結(jié)果。
// Use the Evaluate method to return the evaluated expression.
            Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression));
9. 創(chuàng)建 XPath 表達(dá)式以查找價(jià)格超過(guò) 10 美元的所有圖書(shū)。這個(gè) XPath 表達(dá)式只從 XML 源中返回 Title 節(jié)點(diǎn)。
// Find the title of the books that are greater then $10.00.
            strExpression = "/bookstore/book/title[../price>10.00]";
10. 為使用 XPathNavigator Select 方法選擇的節(jié)點(diǎn)創(chuàng)建 XPathNodeIteratorXPathNodeIterator 表示 XPath 節(jié)點(diǎn)集,因此它支持針對(duì)該節(jié)點(diǎn)集執(zhí)行的操作。
// Select the node and place the results in an iterator.
            NodeIter = nav.Select(strExpression);
11. 使用從 XPathNavigator Select 方法返回的 XPathNodeIterator 遍歷選定的節(jié)點(diǎn)。在這種情況下,可使用 XPathNodeIterator MoveNext 方法迭代通過(guò)選定的所有節(jié)點(diǎn)。
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);
            };
12. 使用 ReadLine 方法在控制臺(tái)顯示的末尾添加 pause,以便更容易地顯示上述結(jié)果。
//Pause
            Console.ReadLine();
13. 生成并運(yùn)行您的項(xiàng)目。請(qǐng)注意,這些結(jié)果顯示在控制臺(tái)窗口中。

疑難解答

在測(cè)試代碼時(shí),您可能會(huì)收到以下異常錯(cuò)誤信息:
An unhandled exception of type ‘System.Xml.XmlException‘ occurred in System.xml.dll

Additional information:System error.
該異常錯(cuò)誤發(fā)生在以下代碼行上:
docNav = new XPathDocument("c:\\books.xml");
該異常錯(cuò)誤是由無(wú)效的處理指令導(dǎo)致的。例如,處理指令可能包含多余的空格。下面是無(wú)效處理指令的示例:
<?xml version=‘1.0‘?>
若要解決該異常,請(qǐng)執(zhí)行以下操作之一:
糾正無(wú)效的處理指令。下面是有效處理指令的示例:
<?xml version=‘1.0‘?>
- 或 -
下面是有效處理指令的示例:從 Books.xml 文件中刪除 XML 處理指令。

參考

下列文件可從 Microsoft 下載中心下載:
下載立即下載 Books.xml (http://download.microsoft.com/download/xml/utility/1.0.0.1/wxp/en-us/books.exe)
有關(guān)更多信息,請(qǐng)?jiān)L問(wèn)以下 Microsoft Web 站點(diǎn):
.NET 中的 XML:.NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation(.NET 中的 XML:.NET 框架 XML 類和 C# 提供簡(jiǎn)單的可縮放的數(shù)據(jù)操作)R/> http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx (http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx)

XPathNavigator Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathNavigatorClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathNavigatorClassTopic.asp)

XPathDocument Class(XPathDocument 類)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathDocumentClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathDocumentClassTopic.asp)

XPathNodeIterator Class(XPathNodeIterator 類)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathNodeIteratorClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathNodeIteratorClassTopic.asp)

XSLT Transformations with the XslTransform Class(使用 XslTransform 類 的 XSLT 轉(zhuǎn)換)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxslttransformationswithxsltransformclass.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxslttransformationswithxsltransformclass.asp)

XPath Examples(XPath 示例)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxpathexamples.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxpathexamples.asp)

XPath Syntax(XPath 語(yǔ)法)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxpathsyntax.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxpathsyntax.asp)
有關(guān) XPath 的更多信息,請(qǐng)?jiān)L問(wèn)下列 WWW 聯(lián)合會(huì) (W3C) Web 站點(diǎn):
XML 路徑語(yǔ)言 (XPath)
1.0 版:W3C 在 1999 年 11 月 16 日提出的建議
http://www./TR/1999/REC-xpath-19991116 (http://www./TR/1999/REC-xpath-19991116)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多