SUMMARY
This step-by-step article shows you how to use the
XmlTextReader class to read Extensible Markup Language (XML) from a Uniform Resource Locator (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 or Microsoft Visual Studio 2005
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. This file is also available for download; refer to the first item in the
References section of this article for the download location.
- Copy the Books.xml file to the \Inetpub\Wwwroot folder on your computer.
- Open Visual Studio .NET or Visual Studio 2005.
- Create a new Visual C# Console Application. You can either proceed to the Complete Code Listing section or continue through these steps to build the application.
- Specify the using directive on the System.Xml namespace so that you are not required to qualify the XmlTextReader class declarations later in your code. You must use the using directive before any other declarations.
using System.Xml;
- Retrieve the XML stream by means of a URL. Streams are used to provide independence from the device; thus, program changes are not required if the source of a stream changes. Declare a constant for the http://localhost/books.xml URL. You will use this constant in the next step with XmlTextReader. Add the following code sample to the Main procedure of the default class:
String URLString = " http://localhost/books.xml";
- 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 the XML. The XmlTextReader class has different constructors to specify the location of the XML data. The following code creates an instance of an XmlTextReader object and passes the URL to the constructor:
XmlTextReader reader = new XmlTextReader (URLString);
- Read through the XML. (Note that this step shows a basic, outer "while" loop, and the next two steps describe 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 obtain the next record. The Read method returns false if there are no more records.
while (reader.Read())
{
// Do some work here on the data.
Console.WriteLine(reader.Name);
}
Console.ReadLine();
- Inspect the nodes. To process the XML data, each record has a node type that 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 example ignores element attributes.
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;
}
}
- 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.
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;
}
}
- Build and run your project.
back to the top
Complete Code Listing
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;
}
}
}
}
}
back to the top
Sample Output
<bookstore>>
<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>
back to the top
REFERENCES
The following file is available for download from the Microsoft Download Center:
For more information, refer to the "XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation" article in
MSDN Magazine at the following Microsoft Web site:
For more information about the
XmlReader class, refer to the following Microsoft .NET Framework Class Library documentation:
For more information about how to use
XmlReader to read XML data, see the following Microsoft .NET Framework Developer's Guide documentation:
back to the top