分享

HOW TO:使用 Visual C

 ShangShujie 2007-05-28

如何从 URL 读取 XML 数据

本示例使用名为 Books.xml 的文件。您可以创建自己的 Books.xml 文件,或者使用 .NET 软件开发工具包 (SDK) 快速入门中包括的示例文件。 还可以下载此文件;有关下载位置的信息,请参阅本文参考部分的第一条。
1. 将 Books.xml 文件复制到计算机上的 \Inetpub\Wwwroot 文件夹中。
2. 打开 Visual Studio .NET。
3. 新建 Visual C# .NET 控制台应用程序。 可以转到完整代码列表一节,也可以继续执行这些步骤以生成应用程序。
4. System.Xml 名称空间上指定 using 指令,这样,以后就不需要在代码中限定 XmlTextReader 类声明了。using 指令必须位于任何其他声明之前。
using System.Xml;
5. 通过 URL 检索 XML 流。 流用于提供与设备之间的独立性,因此,如果流的来源发生变化,并不要求程序也随之变化。 给 http://localhost/books.xml URL 声明一个常量。 在下一步中,将此常量用于 XmlTextReader。 将以下代码示例添加到此默认类的 Main 过程中:
String URLString = " http://localhost/books.xml";
6. 创建 XmlTextReader 类的一个实例并指定 URL。 通常,如果需要将 XML 作为原始数据来访问而不产生文档对象模型 (DOM) 开销,则使用 XmlTextReader;因此,XmlTextReader 提供了一种更快读取 XML 的机制。XmlTextReader 类使用不同的构造函数来指定 XML 数据的位置。 以下代码创建 XmlTextReader 对象的一个实例,并将 URL 传递给构造函数:
XmlTextReader reader = new XmlTextReader (URLString);
7. 读取全部 XML 数据。 (注意,此步骤显示一个基本的外部“while”循环,下两步描述如何使用该循环以及读取 XML。)加载后,XmlTextReader 将连续读取 XML 数据,并使用 Read 方法获取下一条记录。如果没有记录,Read 方法将返回 False。
while (reader.Read())
                                    {
                                    // Do some work here on the data.
                                    Console.WriteLine(reader.Name);
                                    }
                                    Console.ReadLine();
8. 检查节点。若要处理 XML 数据,每个记录都应该有一个可通过 NodeType 属性进行确定的节点类型。Name Value 属性返回当前节点(或记录)的节点名(元素和属性名)和节点值(节点文本)。NodeType 枚举确定节点类型。下面的代码示例显示了元素的名称和文档类型。 注意,此示例忽略了元素属性。
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. 检查属性。元素节点类型可包括一系列与其关联的属性节点。MovetoNextAttribute 方法连续在元素的每个属性中移动。使用 HasAttributes 属性检测节点是否有任何属性。AttributeCount 属性返回当前节点的属性个数。
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=‘autobiography‘ 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>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多