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
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
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
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”);
Usage of XMLTextReader
XMLTextReader class should be used in the following cases:
• Browse through a file for better performance without validation support
| 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 |
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”Copyright - © 2004 - 2019 - All Rights Reserved.