zoukankan      html  css  js  c++  java
  • XML 读取器和编写器从URL读取XML

    如何从 URL 读取 XML

    此示例阐释如何使用 XmlTextReader 类从 URL 读取 XML。

    注意:此示例是如何从文件读取 XML主题的继续。

     
    VB XmlReadFromUrl.aspx

    [运行示例] | [查看源代码]

    XmlTextReader 有不同的构造函数,以指定 XML 数据的位置。此示例从以下特定语言的 URL 之一加载 XmlTextReader:http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/vb/books.xml 或 http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/cs/books.xml。下列示例代码构造一个 XmlTextReader。

    String URLString = "http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/vb/books.xml";
                // Load the XmlTextReader from the URL
                myXmlURLreader = new XmlTextReader (URLString);
                
    C# VB  

    加载完成后,该示例代码调用 FormatXML 函数。在此函数中,XmlTextReader 使用 Read 方法在 XML 数据中移动,按顺序读取数据,以获取下一个节点。如果再没有节点,此函数则返回假。有关 Read 方法的操作原理的更多信息,请参阅如何从文件读取 XML

    while (reader.Read())
                {
                switch (reader.NodeType)
                {
                case XmlNodeType.XmlDeclaration:
                Format (reader, "XmlDeclaration");
                declarationCount++;
                break;
                case XmlNodeType.ProcessingInstruction:
                Format (reader, "ProcessingInstruction");
                piCount++;
                break;
                case XmlNodeType.DocumentType:
                Format (reader, "DocumentType");
                docCount++;
                break;
                case XmlNodeType.Comment:
                Format (reader, "Comment");
                commentCount++;
                break;
                case XmlNodeType.Element:
                Format (reader, "Element");
                elementCount++;
                if (reader.HasAttributes)
                attributeCount += reader.AttributeCount;
                break;
                case XmlNodeType.Text:
                Format (reader, "Text");
                textCount++;
                break;
                case XmlNodeType.Whitespace:
                whitespaceCount++;
                break;
                }
                }
                
    C# VB  

    摘要

    1. XmlTextReader 提供一些构造函数,以从表示 URL 的字符串或文件名、流或 TextReader 读取 XML。
    2. 可使用 MoveToNextAttribute 方法访问属性节点,该方法使您可以确定属性节点的属性。
  • 相关阅读:
    关于在ubuntu12.04图形界面下不能从root用户直接登录的问题
    error: stray '357' in program
    关于gcc -o 的使用问题
    如何解决程序退出重启后不能绑定端口的问题?
    使用Ubuntu12.04登陆账户时,输入密码是正确的,但是图形界面闪一下后就又回到登陆页面了
    如何在linux系统中设置严密的密码策略(译文)
    sqlite3数据库归纳
    Bing地图切片原理
    CSS技巧
    jQuery.extend方法和开发中变量的复用
  • 原文地址:https://www.cnblogs.com/chorrysky/p/584469.html
Copyright © 2011-2022 走看看