XmlTextWriter is derived from the XmlWriter class. XmlWriter is the abstract class that defines the basic functionality required to produce document streams conforming to the W3C's XML recommendations. XmlWriter completely shields applications from the complexities of producing XML document streams by enabling them to work with a well-organized API. Producing a document with XmlWriter is similar to producing documents via SAX. Currently, there are two implementations of XmlWriter : XmlTextWriter and XmlNodeWriter . These implementations are just like reader versions, only they work in the opposite directions.

XmlWriter makes it possible to write out all standard constructs such as elements, attributes, and processing instructions. It does this using the corresponding methods. Some of these methods include WriteStartDocument() , WriteStartElement() , and WriteProcessingInstruction() . XmlWriter also provides methods for writing out typed elements and attributes through the WriteElement() and WriteAttribute() methods. The application page can be seen in Figure 8.2.

Figure 8.2. This page shows how to use XmlWriter to create and write to an XML file.


The source code for this entire part of the application can be seen in Listing 8.4. You need to notice that the first thing I do is create an XmlTextWriter object. I pass the file name as an argument to the constructor. This actually creates the XML file before any data is written. I also set the formatting to be indented. If I don't do this, the entire XML file will be one long line with no character returns ”it then becomes hard to read in any kind of text editor. If your file needs to be opened up or edited with anything besides XmlTextReader you need to be sure you set the formatting property to indented.

The first thing you need to do to create an XML file is called the WriteStart() document method. This prepares the XmlTextWriter for creating a new document. I then call the WriteComment() method, but this is optional and you may not need to do this in your particular application. Another optional method I call is the WriteProcessing() instruction. To create the elements and write the data into these elements, I use a combination of the WriteStartElement() method, the WriteString() method, and the WriteEndElement() method. These create the elements, write the data to them, and then close them. Because I have five elements, I made this set of calls five times: once for the name, once for the address, once for the city, once for the state, and once for the zip. The last thing I had to do was call the WriteEndDocument() method. This closed off the document internally and prepared it for writing to disk. When I finally called the Close() method, all the XML data that was contained in the XmlTextWriter class was saved out to disk.

Listing 8.4 The C# Source Code That Demonstrates a Simple Example Using XMLTextReader
 public void Button1_Click(System.Object sender, System.EventArgs e)  {      try      {          XmlTextWriter Writer = new XmlTextWriter( Request.MapPath ( "SimpleInfo.xml" ), graphics/ccc.gif System.Text.Encoding.Default );          Writer.Formatting = Formatting.Indented;          Writer.WriteStartDocument();          Writer.WriteComment( "Special Edition: Using ASP.NET is a great book!" );          Writer.WriteProcessingInstruction( "Read", "Information" );          Writer.WriteStartElement( "i", "Info", "urn:Info" );          Writer.WriteStartElement( "Name", "" );          Writer.WriteString( TextBox1.Text );          Writer.WriteEndElement();          Writer.WriteStartElement( "Address", "" );          Writer.WriteString( TextBox2.Text );          Writer.WriteEndElement();          Writer.WriteStartElement( "City", "" );          Writer.WriteString( TextBox3.Text );          Writer.WriteEndElement();          Writer.WriteStartElement( "State", "" );          Writer.WriteString( TextBox4.Text );          Writer.WriteEndElement();          Writer.WriteStartElement( "Zip", "" );          Writer.WriteString( TextBox5.Text );          Writer.WriteEndElement();          Writer.WriteEndDocument();          Writer.Close();          Label1.Text = "Done!";      }      catch( Exception ex )      {         Label1.Text = ex.Message;      }  } 

This example also includes a button that enables you to view the raw XML text file. The method I created that does this doesn't use any of the XML classes. It simply opens the file, reads in the lines, and displays them in a label. You can see this code in Listing 8.5. In this code I created a stream reader by calling the file OpenText() method, read through the file one line at a time, and then closed it.

Listing 8.5 This Code Reads in the XML File and Displays It in Raw Format
 public void Button2_Click(System.Object sender, System.EventArgs e)  {      Label2.Text = "";      try      {          StreamReader reader = File.OpenText(Request.MapPath("SimpleInfo.xml"));          while( reader.Peek() != -1 )          {              Label2.Text += ( Server.HtmlEncode( reader.ReadLine() ) + "<br>\r\n" );          }          reader.Close();      }      catch( Exception ex )      {          Label2.Text = ex.Message;      }  } 

The XML data that is written to disk is a very simple file. This file can be seen in Listing 8.6.

Listing 8.6 The Simple XML File That Was Saved Out from the Code in Listing 8.5
 <?xml version="1.0" encoding="Windows-1252"?>  <!--Special Edition: Using ASP.NET is a great book!-->  <?Read Information?>  <i:Info xmlns:i="urn:Info">    <Name>Rick</Name>    <Address>123</Address>    <City>5t</City>    <State>ef</State>    <Zip>asdf</Zip>  </i:Info> 

The XmlTextWriter class is powerful. It enables you to easily save your XML data to any output stream. The example I've used here saves the data to a file, but later examples, especially in Chapter 10, save it to other streams such as SQL Server databases.


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