Writing a Guest Book Application


XML can be used as something more than a means of describing and packaging data. By walking through a fully functional Guest Book application I explain how to combine XML with XSL and ASP.NET to write Web-based applications.

The first impression most developers get after reading about XML is that it sounds great, but they don't know exactly what use it is. In this section I show you a little how XML knowledge combined with ASP.NET and XSL can be used to create a guest book application for your Web site.

Guest books are a simple function of most Web sites. This program can be easily broken down into an XML structure by analyzing the type of information you want to record from visitors , XML is very handy when handling information in a Web site such as:

  • Visitor's name

  • Visitor's email address

  • Visitor's homepage

  • Visitor's country

  • Message

It's also important to record the time and date that the message was left in the guest book. You can take advantage of ASP.NET to get the "real" server time and record this information.

A regular XML document to record time and date could be

 <guestbook>    <entry date="03/11/00 00:03:53">      <name>Rick Leinecker</name>      <email>rick@there.now</email>      <homepage>http://www.infinitevision.NET</homepage>      <country>US</country>      <comment>This is the only comment in the guest book</comment>    </entry>  </guestbook> 

From the example you can see that I can record all the information under each Entry node in the XML document. This is a simple XML document and doesn't really need a Document Type Definition (DTD) to show its structure (or for processing purposes). Guestbook (the document node) can hold many entry nodes, which will eventually make up the entire guest book.

Recording It All

Now that you've seen how I'm going to record the guest book entries in an XML file, it's time to investigate the mechanism that will enable this data to be recorded. Figure 8.5 is a good example of a guest book form where the user inputs information.

Figure 8.5. This page enables the user to add entry information to the guest book.


The majority of guest books are made of a standard HTML form that posts a CGI script (or in some cases, an ASP.NET page). The sample ASP.NET application takes the input from a ASP.NET text box and the controls, and then creates the XML objects on the server.

Follow these steps when you use ASP.NET code to record and store the data by retrieving the information from the user interface controls:

  1. Create an XmlDocument object.

  2. Load the current XML document (with the existing entries) into the XmlDocument object.

  3. Create an XmlDocumentFragment object.

  4. Populate the XmlDocumentFragment object with the text box data that the user entered.

  5. Append the XmlDocumentFragment to the document.

  6. Save the document.

To retrieve the values from the text box controls, I used the following:

  • Name.Text

  • Email.Text

  • Homepage.Text

  • Country.Text

  • Comments.Text

Listing 8.12 shows the ASP.NET code to save the user data. In using this code on your Web site you should place checks to change any of the recorded data (filter out profanities, hide or "munge" visitor email addresses, and so on).

Listing 8.12 The C# Code That Saves the User Information to the XML File
 public void Button1_Click(Object Source, EventArgs e)  {      try      {          XmlDocument doc = new XmlDocument();          doc.Load( Request.MapPath( "gbook.xml" ) );          XmlDocumentFragment newVisitor = doc.CreateDocumentFragment();          newVisitor.InnerXml = "<entry date=\"" + DateTime.Now.Format ("mm/dd/yyyy hh:mm graphics/ccc.gif tt",DateTimeFormatInfo.InvariantInfo) + "\">\r\n" +              "\t<name>" + Name.Text + "</name>\r\n" +              "\t<email>" + Email.Text + "</email>\r\n" +              "\t<homepage>" + Homepage.Text + "</homepage>\r\n" +              "\t<country>" + Country.Text + "</country>\r\n" +              "\t<comment>" + "<![CDATA[" + Comments.Text + "]]></comment>\r\n" +              "</entry>\r\n";          XmlElement root = doc.DocumentElement;          root.AppendChild( newVisitor );          doc.Save( Request.MapPath( "gbook.xml" ) );          Message.Text = "Done!";          Name.Text = "";          Email.Text = "";          Homepage.Text = "";          Country.Text = "";          Comments.Text = "";      }      catch( Exception ex )      {          Message.Text = ex.Message;      }  } 

I have now recorded the visitor information and message inside an XML document stored on the Web Server.

Displaying the Guest Book Entries

In the future, all Internet Browsers will let us view XML and process it using XSL on the client browser. Right now, though, Internet Explorer 5 and above are the only browsers that will do this. You have to process the XML on the server and convert its contents to HTML so that visitors can view the guest book.

Using the XslTransform class I need to transform the XmlDocument class to process the XML file.

When users visit this sample application, they first see one main page. For convenience, I have added a button that enables them to go to the page where the guest book can be viewed . I could have actually placed all the view code in this page, but then I would have had that code in two different pages. So instead, I redirect users who click this button to the view page, as shown in the following code:

 public void Button2_Click(Object Source, EventArgs e)  {      Response.Redirect( "WebForm6.aspx" );  } 

The .aspx code that can be found in the page where users go to view the text book is very straightforward. It is some simple HTML with one additional line. That additional line makes a call to the LoadAndRender() method and the load and render do everything necessary to render the guest book to the display. Now this is a perfect example where code is separated from the actual HTML. This is one of the strong points that ASP.NET brings to the table. The capability to separate code from HTML is something that is long overdue and will be an asset for developers.

 <%@ Page language="c#" Codebehind="WebForm6.cs" AutoEventWireup="false" graphics/ccc.gif Inherits="XmlExamples.WebForm6" %>  <html>    <head>      <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">      <meta name="CODE_LANGUAGE" Content="C#">    </head>    <body>  <% LoadAndRender(); %>    </body>  </html> 

Chapter 11 discusses code-behind in more detail. If this new concept is presenting you with difficulty right now, skip to Chapter 11 and work through it.

You can see the application displayed in the guest book in Figure 8.6. It loads in the XML document and does an XSLT transform.

Figure 8.6. An XSL transform is used to display the guest book.


The rendering is done in the LoadAndRender() method. This method creates an XmlDocument object, loads the XML data into the XmlDocument object, creates an XslTransform object, loads in the XSL file, and then does the transform. One thing you should look at is the XslTransform object's Transform() method. The third argument passed to this method is Response.Output. This causes the transform output to go directly to the HTTP stream if you specify Response.Output as the destination.

 public void LoadAndRender()  {    try    {      XPathDocument doc = new XPathDocument( Request.MapPath( "gbook.xml" ) );      XslTransform xslt = new XslTransform();      xslt.Load( Request.MapPath( "gbook.xsl" ) );      xslt.Transform( doc, null, Response.Output );    }    catch( Exception ex )    {      Response.Write( ex.Message );    }  } 

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