如何從 URL 讀取 XML 數據
本示例使用名為 Books.xml 的文件。您可以創(chuàng )建自己的 Books.xml 文件,或者使用 .NET 軟件開(kāi)發(fā)工具包 (SDK) 快速入門(mén)中包括的示例文件。 還可以下載此文件;有關(guān)下載位置的信息,請參閱本文
參考部分的第一條。
| 1. | 將 Books.xml 文件復制到計算機上的 \Inetpub\Wwwroot 文件夾中。 |
| 2. | 打開(kāi) Visual Studio .NET。 |
| 3. | 新建 Visual C# .NET 控制臺應用程序。 可以轉到完整代碼列表一節,也可以繼續執行這些步驟以生成應用程序。 |
| 4. | 在 System.Xml 名稱(chēng)空間上指定 using 指令,這樣,以后就不需要在代碼中限定 XmlTextReader 類(lèi)聲明了。using 指令必須位于任何其他聲明之前。 using System.Xml; |
| 5. | 通過(guò) URL 檢索 XML 流。 流用于提供與設備之間的獨立性,因此,如果流的來(lái)源發(fā)生變化,并不要求程序也隨之變化。 給 http://localhost/books.xml URL 聲明一個(gè)常量。 在下一步中,將此常量用于 XmlTextReader。 將以下代碼示例添加到此默認類(lèi)的 Main 過(guò)程中: String URLString = " http://localhost/books.xml"; |
| 6. | 創(chuàng )建 XmlTextReader 類(lèi)的一個(gè)實(shí)例并指定 URL。 通常,如果需要將 XML 作為原始數據來(lái)訪(fǎng)問(wèn)而不產(chǎn)生文檔對象模型 (DOM) 開(kāi)銷(xiāo),則使用 XmlTextReader;因此,XmlTextReader 提供了一種更快讀取 XML 的機制。XmlTextReader 類(lèi)使用不同的構造函數來(lái)指定 XML 數據的位置。 以下代碼創(chuàng )建 XmlTextReader 對象的一個(gè)實(shí)例,并將 URL 傳遞給構造函數: XmlTextReader reader = new XmlTextReader (URLString); |
| 7. | 讀取全部 XML 數據。 (注意,此步驟顯示一個(gè)基本的外部“while”循環(huán),下兩步描述如何使用該循環(huán)以及讀取 XML。)加載后,XmlTextReader 將連續讀取 XML 數據,并使用 Read 方法獲取下一條記錄。如果沒(méi)有記錄,Read 方法將返回 False。 while (reader.Read()) { // Do some work here on the data. Console.WriteLine(reader.Name); } Console.ReadLine(); |
| 8. | 檢查節點(diǎn)。若要處理 XML 數據,每個(gè)記錄都應該有一個(gè)可通過(guò) NodeType 屬性進(jìn)行確定的節點(diǎn)類(lèi)型。Name 和 Value 屬性返回當前節點(diǎn)(或記錄)的節點(diǎn)名(元素和屬性名)和節點(diǎn)值(節點(diǎn)文本)。NodeType 枚舉確定節點(diǎn)類(lèi)型。下面的代碼示例顯示了元素的名稱(chēng)和文檔類(lèi)型。 注意,此示例忽略了元素屬性。 while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display the end of the element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } |
| 9. | 檢查屬性。元素節點(diǎn)類(lèi)型可包括一系列與其關(guān)聯(lián)的屬性節點(diǎn)。MovetoNextAttribute 方法連續在元素的每個(gè)屬性中移動(dòng)。使用 HasAttributes 屬性檢測節點(diǎn)是否有任何屬性。AttributeCount 屬性返回當前節點(diǎn)的屬性個(gè)數。 while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read the attributes. Console.Write(" " + reader.Name + "=‘" + reader.Value + "‘"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display the end of the element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } |
| 10. | 生成并運行您的項目。 |
完整代碼列表
using System; using System.Xml; namespace ReadXMLfromURL { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { static void Main(string[] args) { String URLString = "http://localhost/books.xml"; XmlTextReader reader = new XmlTextReader (URLString); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read the attributes. Console.Write(" " + reader.Name + "=‘" + reader.Value + "‘"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display the end of the element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
<book genre=‘a(chǎn)utobiography‘ publicationdate=‘1981‘ 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‘ 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‘ ISBN=‘1-861001-57-6‘>> <title>> The Gorgias </title> <author>> <name>> Plato </name> </author> <price>> 9.99 </price> </book> </bookstore>