XML reader and writer -- read XML from URL

Source: Internet
Author: User
Tags xml reader
How to read XML from a URL

This example illustrates how to use the xmltextreader class to read XML from a URL.

Note: This example shows how to continue reading the XML topic from a file.


VB xmlreadfromurl. aspx

[Running example] | [View Source Code]

Xmltextreader has different constructors to specify the location of XML data. This example loads xmltextreader: http: // localhost/Quickstart/howto/samples/XML/xmlreadfromurl/Vb/books from one of the URLs in the following languages. XML or http: // localhost/Quickstart/howto/samples/XML/xmlreadfromurl/CS/books. XML. The following sample code constructs an xmltextreader.

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

After loading, the sample code calls the formatxml function. In this function, xmltextreader uses the read method to move XML data and read data in order to obtain the next node. If no node exists, this function returns false. For more information about the operating principles of the read method, see how to read XML from a file

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;            }            }            
While reader.Read()            Select (reader.NodeType)            case XmlNodeType.XmlDeclaration:            Format (reader, "XmlDeclaration")            declarationCount += 1            case XmlNodeType.ProcessingInstruction:            Format (reader, "ProcessingInstruction")            piCount += 1            case XmlNodeType.DocumentType:            Format (reader, "DocumentType")            docCount += 1            case XmlNodeType.Comment:            Format (reader, "Comment")            commentCount += 1            case XmlNodeType.Element:            Format (reader, "Element")            elementCount += 1            if (reader.HasAttributes)            attributeCount += reader.AttributeCount            end if            case XmlNodeType.Text:            Format (reader, "Text")            textCount += 1            case XmlNodeType.Whitespace:            whitespaceCount += 1            End Select            End While            
C # VB  

  1. Xmltextreader provides constructors to read XML from strings, file names, streams, or textreader that represent URLs.
  2. You can use the movetonextattribute method to access attribute nodes. This method allows you to determine the attributes of attribute nodes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.