Manage Learn to apply best practices and optimize your operations.

Using an XML reader

Code for such a reader.

.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)

Using an XML reader
Dan Fox

If you've been using XML for document transfers and integration to backend processes in your company, then you need to be able to read them and transfer them from one place to the other. This tip, excerpted from InformIT, discusses how to use an XML reader to gobble up those XML docs and get them to where they're supposed to go.

Dan Fox is the author of Building Distributed Applications with Visual Basic .NET, published by Quilogy, Inc. The article from which this tip comes is included in a chapter of this book.

When the DOM was first published and vendors such as Microsoft began writing parsers like MSXML to read those documents, it became immediately apparent that the programming model was not ideal for all applications. This was particularly the case when the XML document was large, since by definition the DOM represents the entire document in an in-memory tree structure. Not only did performance suffer using the DOM since you had to wait for the document to be parsed and loaded, but the application processing the document tended to eat up significant amounts of memory. As a result, Microsoft included the SAX APIs in MSXML 3.0 to provide an event-driven programming model for XML documents. While this alleviated the performance and memory constraints of the DOM, it did so at the cost of complexity.

The XmlReader implementation is in many ways a melding of the DOM and SAX and provides a simplified programming model like the DOM but in a stream-based architecture. The programming model is simplified because it is a pull rather than a push model. In other words, developers pull data from the document using a familiar cursor-style looping construct rather than simply being pushed data by responding to events fired from the parser.

The XmlReader class is actually an abstract base class for the XmlTextReader, XmlValidatingReader, and XmlNodeReader classes. The XmlReader is often used as the input or output arguments for other methods in the Services Framework. A typical use of the XmlTextReader is to read XML produced from the FOR XML statement in SQL Server 2000 and then load a DataSet object as shown in the following code:

Imports System.Xml
Imports System.Data.SqlClient
Imports System.Data

Dim cn As New SqlConnection("server=ssosa;database=enrollment;uid=sa")
Dim cm As New SqlCommand( _
 "SELECT FName, LName, Company FROM Student FOR XML AUTO, XMLDATA", cn)
Dim xmlr As XmlReader
Dim ds As New DataSet()

cm.CommandType = CommandType.Text
xmlr = cm.ExecuteXmlReader
ds.ReadXml(xmlr, XmlReadMode.Fragment)

Note that the ExecuteXmlReader method of the SqlCommand object returns an XmlReader which is then accepted as an argument to one of the overloaded ReadXml methods of the DataSet object. As discussed in Chapter 7, the second argument to the ReadXml method specifies how the XML is parsed using one of the XmlReadMode constants.

As implied by the names, XmlTextReader simply provides a reader that checks for well-formedness of an XML document and any inline DTDs but does not perform validation using an associated DTD or schema as does XmlValidatingReader. As a result, XmlTextReader is the fastest way to parse an XML document using a file, Stream, or TextReader as input. XmlNodeReader, on the other hand, can parse XmlNode objects from an XML DOM subtree.

To read the entire tip from which this article is excerpted, click over to InformIT. You have to register there, but the registration is free.

For More Information:

  • Looking for free research? Browse our comprehensive White Papers section by topic, author or keyword.
  • Are you tired of technospeak? The Web Services Advisor column uses plain talk without the hype.
  • For insightful opinion and commentary from today's industry leaders, read our Guest Commentary columns.
  • Hey Codeheads! Start benefiting from other time-saving XML Developer Tips and .NET Developer Tips.
  • Visit our huge Best Web Links for Web Services collection for the freshest editor-selected resources.
  • Choking on the alphabet soup of industry acronyms? Visit our helpful Glossary for the latest lingo.
  • Visit Ask the Experts for answers to your Web services, SOAP, WSDL, XML, .NET, Java and EAI questions.
  • Discuss this issue, voice your opinion or just talk with your peers in the SearchWebServices Discussion Forums.

Dig Deeper on Topics Archive

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.