Using XMLTextReader Class for Better Access to XML

With the advent of internet technology and web services, information exchange has become a key concern for the web-based applications. HTML (Hyper Text Markup Language) proved initially to be a great tool for communication mainly due to its simplicity and acceptance from all the browsers but later, it was not flexible for handling large volumes of data.

XML (eXtensible Markup Language) has now become a better choice than HTML due to its flexibility to present the data in a simple, structured and self-explanatory manner and hence become a global standard. While HTML uses tags to represent data as items to display, XML allows using meaningful tags that immediately precedes the actual data and hence improves the readability and makes it easier for data manipulation and exchange.

Until the dawn of .Net, Microsoft provided the MSXML parser that includes XMLDOM and SAX parsers for manipulating XML data. This parser supports asynchronous loading and validation while parsing XML but there is no such direct support in .Net. However, the architecture of .Net framework is such that it uses XML for its own functionalities like configuration and documentation files. To support itself and allow user to leverage its functionalities, Net framework provides core XML functionalities as classes like:

• XML readers
• XML writers
• XML document classes
• Serialization classes

All these classes are included in the System.Xml namespace. Hence, it is necessary to replace the following line of code (which is used for using MSXML parser)

Using MSXML2 …. with
Using System.Xml

Even though .Net allows you to use the MSXML 3.0 parser, it is preferable to use the System.XML namespace for its flexible nature and ease of use. System.Xml has its own hierarchy of classes for reading, writing and validating XML. You should carefully decide to choose the right class for XML operations since there are multiple ways of implementing the same task. For example, to just go through an XML document once, it is better to use XMLTextReader class instead of XMLDocument since the latter loads the document on to the disk which is really not necessary.


XMLTextReader class is derived from an abstract class, XMLReader. It allows streaming the XML data for processing in a fast, forward-only and read-only mode. It is designed as a ‘pull-type’ model, which means selected data can be extracted, on need basis by the application requesting it and hence does not demand more memory. On the other hand, there is no facility for navigation to scroll in the backward direction, search and validation against schema or DTD (Document Type Definition).

XMLTextReader has different overloaded constructors for creating and initializing the objects with different types of data (like file names, streams, URL identifying a web location, etc.). After initialization, there is no current node. The first call to Read method will move the cursor to first node in document which would typically be an XML declaration node.

XMLTextReader has methods to:

• Check whether the current content node is a tag
• Read the next node from the stream
• Read simple, text only elements
• Read the current element value as required type like String, Decimal, Boolean, etc.
• Check whether the current node is a content node and if not so, move the cursor to the next node or to the end of file.
• Get the value of an attribute by name or index
• Compare a specified object with the current object of the cursor

While using XMLTextReader class, it is better to use XMLException class to catch for exceptions thrown while parsing. Errors occur when the XML data is not structured as per the rules defined in W3C recommendation.

Sample code to illustrate the usage of XMLTextReader:

//Following is a code (in C#) snippet to illustrate the usage of XMLTextReader class. This code gets all the items specified in a particular node of an XML file and fills a string.

XMLTextReader textObj = new XMLTextReader(“C:\text.xml”);
String tempStr;

//read the data until the end of file and store in the string variable
while (textObj.Read())
If (textObj.Value != NULL)
tempStr += “\n” + textObj.Value;
Console.WriteLine (tempStr) ; //print the value

catch(XMLException e)
Console.WriteLine( e.Message);

Usage of XMLTextReader

XMLTextReader class should be used in the following cases:

• Browse through a file for better performance without validation support
• Read the XML file ONLY in forward direction and sequentially without searching in random order
• Read entirely a small XML file whose size is less than two gigabytes
• Check only if the XML content is well-formed but not validate against any schema or DTD


SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.


| An Introduction to Oracle’s JD Edwards EnterpriseOne Tools | Avoiding-sql-injection-in-pl-sql-oracle1og | Developing PL/SQL Web Applications in Oracle10g: An Overview | Identifying Rows by Address in Oracle 10g | Introducing Oracle OLAP option to Oracle Database 11g | Some Exciting New Features of Oracle11g | The Benefits of Partition for Improved Performance in Oracle Database 11g | Understanding Automatic SQL Tuning in Oracle10g | Using XMLTextReader Class for Better Access to XML |



FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.



Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.



Add to My Yahoo!

Visit XML Training Material Guide Homepage




“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.