Microsoft KB Archive/301225

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

Article ID: 301225

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 Q301225







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

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

REFERENCES



SUMMARY
This article demonstrates how to use the XmlTextReader class to read Extensible Markup Language (XML) from a file. XmlTextReader provides direct parsing and tokenizing of XML and implements the XML 1.0 specification as well as the namespaces in XML specification from the World Wide Web Consortium (W3C). This article provides fast, tokenized stream access to XML rather than using an object model such as the XML Document Object Model (DOM).

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 an XML file

back to the top

How to read XML from a file
This example uses a file named Books.xml. You can create your own Books.xml file. You must copy Books.xml to the \Bin\Debug folder that is located under the folder in which you create this project. Books.xml is also available for download; refer to the References section for the download location.  Open Visual Studio .NET Create a new Visual Basic .NET Console Application. You can proceed directly to the Complete Code Listing section or continue through these steps to build the application. Make sure that the project contains a reference to the System.Xml.dll assembly.  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   Construct an XmlTextReader class with the XML file. Typically, XmlTextReader is used if you need to access the XML as raw data without the overhead of a 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 the Books.xml file. Add the following code to construct XmlTextReader in the Main procedure of Module1: Dim reader As XmlTextReader = New XmlTextReader (&quot;books.xml&quot;)   Read through the XML. Note that this step shows an 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 sample code displays the name of the elements and the document type. Note that this sample 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> Save and close your project.</li></ol>

back to the top

Complete code listing
Imports System.Xml Module Module1

Sub Main Dim reader As XmlTextReader = New XmlTextReader(&quot;books.xml&quot;)

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 Console.ReadLine

End Sub

End Module

Output
The Autobiography of Benjamin Franklin <first-name> Benjamin </first-name> <last-name> Franklin </last-name> 8.99 The Confidence Man <first-name> Herman </first-name> <last-name> Melville </last-name> 11.99 The Gorgias Plato 9.99 back to the top

<div class="references_section">