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 that 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. The XmlReader demo program compares using the pull model to using the push model. 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 9.1, which includes three book authors and book titles. We're going to use this file to demonstrate XmlReader.

Listing 9.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 Coontz</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.ASPNET-Solutions.com. After you get to the Web site, go to the chapter examples, and then select Chapter 9. One of the first examples is entitled XmlReader. Click the XmlReader link to see the example that I have prepared to demonstrate as simple use of XmlReader.

The first thing you should do is click the Read and Render Xml File button. When you click the button, an XmlReader goes out and reads the Simple.xml file that you saw in Listing 9.1 and renders it on the screen.

The source code for this example is shown in Listing 9.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 note 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. You can see this part of the application in Figure 9.1.

Figure 9.1. This Page Reads and Renders an XML Ril, and Also Shows the Raw XML.


One of the most important things you need to note is the while. 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 9.2 The C# Source Code That Demonstrates a Simple Example Using XML Text Reader


 public void ShowXmlRead_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" );         }     } } 


 Private Sub ShowXmlRead_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click     Dim Reader As New XmlTextReader(Request.MapPath("Simple.xml"))     Label1.Text = ""     While Reader.Read()         If Reader.NodeType = XmlNodeType.Text Then             Label1.Text += ( Reader.Value + "<br>\r\n" )         End If     While End End Sub 

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

If you look at the code, you will notice I don't use any XML classes. I actually used the .NET StreamReader 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.

Listing 9.3 Here Is the C# Code That Is Used to Display Raw XML Text.


 public void DisplayRaw_Click(System.Object sender, System.EventArgs e) {     Label2.Text = "";     try     {         StreamReader reader =           File.OpenText(Request.MapPath("SimpleXml.txt"));         // Label2 eventually renders as a <span> in the HTML.         Label2.Text += "<pre>";         while( reader.Peek() != -1 )         {             Label2.Text += ( Server.HtmlEncode( reader.ReadLine() ) +               "<br>\r\n" );         }         Label2.Text += "</pre>";         reader.Close();     }     catch( Exception ex )     {         Label2.Text = ex.Message.ToString();     } } 


 Private Sub DisplayRaw_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click     Label2.Text = ""     Try         StreamReader reader = _           File.OpenText(Request.MapPath("SimpleXml.txt"))         ' Label2 eventually renders as a <span> in the HTML.         While reader.Peek() != -1             Label2.Text += "<pre>"             Label2.Text += ( Server.HtmlEncode( reader.ReadLine() ) + _               "<br>\r\n" );             Label2.Text += "</pre>"         End While         reader.Close()     Catch ex As Exception         Label2.Text = ex.Message.ToString()     End Try End Sub 

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.

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175

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