如何從 URL 讀取 XML 數(shù)據(jù)
本示例使用名為 Books.xml 的文件。您可以創(chuàng)建自己的 Books.xml 文件,或者使用 .NET 軟件開發(fā)工具包 (SDK) 快速入門中包括的示例文件。 還可以下載此文件;有關下載位置的信息,請參閱本文
參考部分的第一條。
| 1. |
將 Books.xml 文件復制到計算機上的 \Inetpub\Wwwroot 文件夾中。
|
| 2. |
打開 Visual Studio .NET。
|
| 3. |
新建 Visual C# .NET 控制臺應用程序。 可以轉(zhuǎn)到完整代碼列表一節(jié),也可以繼續(xù)執(zhí)行這些步驟以生成應用程序。
|
| 4. |
在 System.Xml 名稱空間上指定 using 指令,這樣,以后就不需要在代碼中限定 XmlTextReader 類聲明了。using 指令必須位于任何其他聲明之前。
using System.Xml;
|
| 5. |
通過 URL 檢索 XML 流。 流用于提供與設備之間的獨立性,因此,如果流的來源發(fā)生變化,并不要求程序也隨之變化。 給 http://localhost/books.xml URL 聲明一個常量。 在下一步中,將此常量用于 XmlTextReader。 將以下代碼示例添加到此默認類的 Main 過程中:
String URLString = " http://localhost/books.xml";
|
| 6. |
創(chuàng)建 XmlTextReader 類的一個實例并指定 URL。 通常,如果需要將 XML 作為原始數(shù)據(jù)來訪問而不產(chǎn)生文檔對象模型 (DOM) 開銷,則使用 XmlTextReader;因此,XmlTextReader 提供了一種更快讀取 XML 的機制。XmlTextReader 類使用不同的構造函數(shù)來指定 XML 數(shù)據(jù)的位置。 以下代碼創(chuàng)建 XmlTextReader 對象的一個實例,并將 URL 傳遞給構造函數(shù):
XmlTextReader reader = new XmlTextReader (URLString);
|
| 7. |
讀取全部 XML 數(shù)據(jù)。 (注意,此步驟顯示一個基本的外部“while”循環(huán),下兩步描述如何使用該循環(huán)以及讀取 XML。)加載后,XmlTextReader 將連續(xù)讀取 XML 數(shù)據(jù),并使用 Read 方法獲取下一條記錄。如果沒有記錄,Read 方法將返回 False。
while (reader.Read())
{
// Do some work here on the data.
Console.WriteLine(reader.Name);
}
Console.ReadLine();
|
| 8. |
檢查節(jié)點。若要處理 XML 數(shù)據(jù),每個記錄都應該有一個可通過 NodeType 屬性進行確定的節(jié)點類型。Name 和 Value 屬性返回當前節(jié)點(或記錄)的節(jié)點名(元素和屬性名)和節(jié)點值(節(jié)點文本)。NodeType 枚舉確定節(jié)點類型。下面的代碼示例顯示了元素的名稱和文檔類型。 注意,此示例忽略了元素屬性。
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. |
檢查屬性。元素節(jié)點類型可包括一系列與其關聯(lián)的屬性節(jié)點。MovetoNextAttribute 方法連續(xù)在元素的每個屬性中移動。使用 HasAttributes 屬性檢測節(jié)點是否有任何屬性。AttributeCount 屬性返回當前節(jié)點的屬性個數(shù)。
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>