Microsoft KB Archive/301232

= How to read XML data from a URL by using Visual Basic .NET =

Article ID: 301232

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q301232







For a Microsoft Visual C# .NET version of this article, see 307643.

IN THIS TASK
SUMMARY
 * Requirements
 * How to Read XML Data from a URL
 * Complete Code Listing

REFERENCES



SUMMARY
This article demonstrates how to use the XmlTextReader class to read Extensible Markup Language (XML) from a URL. The streamed information can come from a variety of sources, such as a byte stream from a server, a file, or a TextReader class.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * XML terminology
 * Creating and reading XML
 * URLs and creating an XML endpoint

back to the top

How to read XML data from a URL
This example uses a file named Books.xml. You can create your own Books.xml file or use the sample file that is included with the .NET Software Development Kit (SDK) QuickStarts. Books.xml is also available for download; refer to the References section for the download location.  Copy Books.xml to the \Inetpub\Wwwroot folder on your computer. Open Visual Studio .NET Create a new Visual Basic .NET Console Application. You can proceed to the complete code listing or continue through these steps to build the application. Make sure that the project contains a reference to the System.Xml namespace.  Use the Imports statement on the Xml namespace so that you are not required to qualify XmlTextReader declarations later in your code. You must use the Imports statement prior to any other declarations. Imports System.Xml   Retrieve the XML stream through a URL. Streams are used to provide independence from the device; thus, no program changes are required if the source of a stream changes. Declare a constant for the http://localhost/books.xml URL. You will use this in the next step with the XmlTextReader class. Add the following code sample to the Main procedure of the default module: const URLString as String = &quot;http://localhost/books.xml&quot; </li>  Create an instance of the XmlTextReader class, and specify the URL. Typically, XmlTextReader is used if you need to access the XML as raw data without the overhead of a Document Object Model (DOM); thus, XmlTextReader provides a faster mechanism for reading XML. XmlTextReader has different constructors to specify the location of the XML data. The following code loads XmlTextReader from a URL: Dim reader As XmlTextReader = New XmlTextReader (URLString) </li>  Read through the XML. (Note that this step shows a basic, outer &quot;while&quot; loop, and the next two steps show how to use that loop and read XML.) After it is loaded, XmlTextReader performs sequential reads to move across the XML data and uses the Read method to get the next record. The Read method returns false if there are no more records. Do While (reader.Read) ' Do some work here on the data. Console.WriteLine(reader.Name) Loop

' Reading of the XML file has finished. Console.ReadLine 'Pause </li>  Inspect the nodes. To process the XML data, each record has a node type, which can be determined from the NodeType property. The Name and Value properties return the node name (the element and attribute names) and the node value (the node text) of the current node (or record). The NodeType enumeration determines the node type. The following example displays the name of the elements and the document type. Note that this example ignores element attributes. Do While (reader.Read) Select Case reader.NodeType Case XmlNodeType.Element 'Display beginning of element. Console.Write(&quot;<&quot; + reader.Name) Console.WriteLine(&quot;>&quot;) Case XmlNodeType.Text 'Display the text in each element. Console.WriteLine(reader.Value) Case XmlNodeType.EndElement 'Display end of element. Console.Write(&quot;</&quot; + reader.Name) Console.WriteLine(&quot;>&quot;) End Select Loop </li>  Inspect the attributes. Element node types can include a list of attribute nodes that are associated with them. The MovetoNextAttribute method moves sequentially through each attribute in the element. Use the HasAttributes property to test whether the node has any attributes. The AttributeCount property returns the number of attributes for the current node. Do While (reader.Read) Select Case reader.NodeType Case XmlNodeType.Element 'Display beginning of element. Console.Write(&quot;<&quot; + reader.Name) If reader.HasAttributes Then 'If attributes exist While reader.MoveToNextAttribute 'Display attribute name and value. Console.Write(&quot; {0}='{1}'&quot;, reader.Name, reader.Value) End While End If Console.WriteLine(&quot;>&quot;) Case XmlNodeType.Text 'Display the text in each element. Console.WriteLine(reader.Value) Case XmlNodeType.EndElement 'Display end of element. Console.Write(&quot;</&quot; + reader.Name) Console.WriteLine(&quot;>&quot;) End Select Loop </li> Build and run your project.</li></ol>

back to the top

Complete code listing
Imports System.Xml Module Module1

Sub Main Const URLString As String = &quot;http://localhost/books.xml&quot; Dim reader As XmlTextReader = New XmlTextReader(URLString) Do While (reader.Read) Select Case reader.NodeType Case XmlNodeType.Element 'Display beginning of element. Console.Write(&quot;<&quot; + reader.Name) If reader.HasAttributes Then 'If attributes exist While reader.MoveToNextAttribute 'Display attribute name and value. Console.Write(&quot; {0}='{1}'&quot;, reader.Name, reader.Value) End While End If                   Console.WriteLine(&quot;>&quot;) Case XmlNodeType.Text 'Display the text in each element. Console.WriteLine(reader.Value) Case XmlNodeType.EndElement 'Display end of element. Console.Write(&quot;</&quot; + reader.Name) Console.WriteLine(&quot;>&quot;) End Select Loop

End Sub

End Module back to the top

<div class="references_section">