XmlTextReader

   

XmlTextReader

The XmlTextReader class is derived from the XmlReader class. XmlReader provides a fast forward-only cursor for reading XML documents. It simplifies XML by providing some well-defined methods . XmlReader also implements a pull model which has several advantages over the more common push model. First and foremost, the pull model is easier to use. It is just easier for developers to think about while loops than complicated state machines. Although contextual state management is still a challenge with the pull model, it is easier to deal with through procedural techniques that are more natural for most developers.

The pull model can also offer improved performance through a variety of techniques. The XmlReader class makes efficient use of character buffers. In the end, the pull model offers a more familiar programming model along with performance benefits.

This section talks about the XmlTextReader class and shows some examples of using it. Take a look at the simple XML file in Listing 8.1, which includes three book authors and book titles. This file contains three books with their authors, and we're going to use this file to demonstrate XmlReader.

Listing 8.1 An XML File Representing Authors and Books
 <?xml version="1.0" encoding="utf-8" ?>  <library>   <book>    <author>Ernest Hemingway</author>    <title>For Whom the Bell Tolls</title>   </book>   <book>    <author>Dean Koontz</author>    <title>Watchers</title>   </book>   <book>    <author>Tom Clancy</author>    <title>The Cardinal Of The Kremlin</title>   </book>  </library> 

I have created an example, which you can see at www.UsingASP.NET. After you get to the Web site, go to the chapter examples and then select Chapter 8. One of the first examples is entitled XmlReader . Click on the XmlReader link to see the example that I have prepared to demonstrate a simple use of XmlReader .

The first thing you should do is click on the Read and Render XML file button (you can see the screen in Figure 8.1). When you click on the button, an XmlReader goes out and reads the Simple.xml file that you saw in Listing 8.1 and renders it on the screen. You can see this application running in Figure 8.1.

Figure 8.1. This page reads and renders an XML file, and also shows the raw XML.

graphics/08fig01.gif

The source code for this example is shown in Listing 8.2. The code is very simple. First XmlTextReader is instantiated using the constructor, which takes a single argument. This argument is a string, which indicates the XML file that will be opened and read. Notice that I am using the Request.MapPath() method so that what gets past to the XmlTextReader constructor will be a fully qualified path .

My .aspx file has a text label that I am going to use for display purposes. I will read in the XML and if the node type is text I will put the value into the label with a <br> at the end of it. The <br> ensures that each XML item that is read in is on a single line.

One of the most important things you need to note is the while loop. You will see while reader.read() do something. The Reader.Read() method reads through the XML data until there is no more data left. This provides an extremely easy, straightforward, and understandable way to process and read XML files.

Listing 8.2 The C# Source Code That Demonstrates a Simple Example Using XMLTextReader
 public void Button1_Click(System.Object sender, System.EventArgs e)  {     XmlTextReader Reader = new XmlTextReader( Request.MapPath( "Simple.xml" ) );      Label1.Text = "";      while( Reader.Read() )      {          if( Reader.NodeType == XmlNodeType.Text )          {              Label1.Text += ( Reader.Value + "<br>\r\n" );          }      }  } 

I have also created, on the same Web page, a simple program that reads the XML data file, as is, and displays it on the screen. This way, users can see what the actual XML that was used to create the rendering with XmlTextReader looks like. You can see this code in Listing 8.3.

If you look at the code, you will notice I don't use any XML classes. I actually used the .NET stream reader and file classes to read in the wrong XML from the disk file. I did this so that there would be no processing of XML and users could see the raw XML data. The stream reader and file classes were covered in Chapter 5. For more information take a look at Chapter 5.

Listing 8.3 The C# Code That Is Used to Display Raw XML Text
 public void Button2_Click(System.Object sender, System.EventArgs e)  {      Label2.Text = "";      try      {          StreamReader reader = File.OpenText(Request.MapPath("SimpleXml.txt"));          while( reader.Peek() != -1 )          {              Label2.Text += ( Server.HtmlEncode( reader.ReadLine() ) + "<br>\r\n" );          }          reader.Close();      }      catch( Exception ex )      {          Label2.Text = ex.Message;      }  } 

The XmlTextReader , which is derived from XmlReader , provides you with an easy, clean way to parse data from XML files. It supports other types of input streams, though, such as those from a database. It also provides data valuation and resolution of external entities. XMLTextReader is easy to use. You should probably take a few moments to create a simple program of your own to familiarize yourself with using the class.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net