No Box Solutions
Home | Articles

The XmlTextReader Constructor

Denise Wynn
August 10, 2005

The System.Xml.XmlTextReader is a great alternative to the XmlDocument object. The XmlTextReader is used in situations where you only need to read the data once and will not be manipulating the xml or referring to it multiple times.

The XmlTextReader constructor has a long overload list and it can be confusing to know what constructor to use. In this article we'll be going over several of these constructors with examples.

XmlTextReader(Stream)

The XmlTextReader(Stream) constructor takes a stream of xml data as input. Below is an example that uses WebRequest and WebResponse to get a stream from a url.

WebRequest request = 
    WebRequest.Create("http://localhost/myxmldata.xml"); 
WebResponse response = request.GetResponse(); 

//Get the Response Stream from the URL 
Stream responseStream = response.GetResponseStream(); 

//Read the Stream using XmlTextReader 
XmlTextReader reader = new XmlTextReader(responseStream);

XmlTextReader(string)

A lot of people have confused the XmlTextReader(string) constructor as taking a string of xml data but what it's actually looking for is a url string that points to the location of the xml data. The base uri is also set to the specified url.

XmlTextReader reader = 
    new XmlTextReader(Server.MapPath("myxmldata.xml"));

Or

XmlTextReader reader = 
    new XmlTextReader("http://localhost/myxmldata.xml");

One possible usage for the XmlTextReader(Stream) and XmlTextReader(string) constructors would be to read RSS feeds from websites.

XmlTextReader(string, Stream)

If the xml data you are streaming has references to external sources you can use the XmlTextReader(string, Stream) constructor to specify the url string to the external source as well as the stream.

string path = http://localhost/myxmldata.xml;

WebRequest request = WebRequest.Create(path); 
WebResponse response = request.GetResponse(); 

//Get the Response Stream from the URL 
Stream responseStream = response.GetResponseStream(); 

//Read the Stream using XmlTextReader 
XmlTextReader reader = new XmlTextReader(path, responseStream);

XmlTextReader will set the BaseUri to the specified string path.

If you know that the xml data you are reading does not have any references to external resources then you can just use the XmlTextReader(Stream) constructor. If you are unsure if the xml data has external references or not then using the XmlTextReader(string, Stream) constructor could help prevent errors.

XmlTextReader(Stream, XmlNodeType, XmlParserContext)

XmlTextReader(Stream, XmlNodeType, XmlParserContext) allows you to do several different things. For example, setting the XmlNodeType to XmlNodeType.Document tells the reader that the xml fragment in the stream is an entire xml document and will enforce document level rules on the data. If you set the XmlNodeType to XmlNodeType.Element then the reader does not see the xml data as an entire xml document and does not enforce document level rules. The former is useful if you want to enforce document level rules but the later is very useful if you can’t verify that the xml data you are receiving is a complete xml document.

XmlTextReader reader = 
    new XmlTextReader(responseStream, XmlNodeType.Document, null);

Or

XmlTextReader reader = 
    new XmlTextReader(responseStream, XmlNodeType.Element, null);

The XmlParserContext can be used to specify a variety of context information that the reader can use to parse the xml data. In the following example the XmlParserContext is being used to specify the type of character encoding.

//Create the XmlParserContext with encoding
XmlParserContext context = 
    new XmlParserContext(null, null, string.Empty, XmlSpace.None, Encoding.UTF8);

//Read the Response Stream using XmlTextReader 
XmlTextReader reader = 
    new XmlTextReader(responseStream, XmlNodeType.Element, context);

You can also use the XmlParserContext to specify the base uri,

XmlTextReader(string, XmlNodeType, XmlParserContext)

If you really need to read a string of xml data into an XmlTextReader then your only option is to use the XmlTextReader(string, XmlNodeType, XmlParserContext) constructor. This is the only XmlTextReader constructor that takes a string of xml data instead of a url to xml data.

//a string of xml
string xml = "text in node";
 
//Read the string using XmlTextReader  
XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Document, null);

In Conclusion

This article showed you several ways to initialize an XmlTextReader for a variety of uses. If you are interested in additional information on this topic check out the official MSDN XmlTextReader Constructor documentation.

Copyright © 2008 No Box Solutions