Article
Create Your Own Guestbook In ASP.NET
The Saving Process
The function SaveXMLData() saves the information for us. As we're using an XML database to store the information, we use the XmlDocument, XmlElement and XmlText classes, which provide all the functions we need.
Next, we create a new XMLDocument class object and load the guestbook.xml file. The required nodes are created with the function CreateElement, and the information entered by the user is retrieved and stored to an object of XmlText. Next, we store the created nodes without any values, using the function AppendChild in conjunction with the main XmlDocument object.
And finally, the values are stored in the nodes we just created, we save all changes to the guestbook.xml file, and we redirect the page to viewguestbook.aspx, where the stored comment is displayed.
Viewing the Guestbook
To view the guestbook, we must created an another Web form:
<% @Page Language="C#" Debug="true" Src="ViewGuestbook.cs"
Inherits="ViewGuestbook" %>
As you see, this Web form doesn't really do all that much. It simply calls the codebehind file, ViewGuestbook.cs. Let's take a look at this file.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
public class ViewGuestbook : Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//Load the XML file
XmlDocument doc = new XmlDocument( );
doc.Load( Server.MapPath("guestbook.xml") );
//Load the XSL file
XslTransform xslt = new XslTransform();
xslt.Load( Server.MapPath("guestbook.xsl") );
string xmlQuery="//guestbook";
XmlNodeList nodeList=doc.Document
Element.SelectNodes(xmlQuery);
MemoryStream ms=new MemoryStream();
xslt.Transform( doc, null, ms);
ms.Seek( 0, SeekOrigin.Begin );
StreamReader sr = new StreamReader(ms);
//Print out the result
Response.Write(sr.ReadToEnd());
}
}
I've created this class to display all comments submitted through the guestbook to our users. Again, the first thing we do is implement the required namespaces, and, as we're using XSL for the visualisation, we have to be sure to include the namespace System.Xml.Xsl.
Then we create a new class called ViewGuestbook, with a private inbuilt function called Page_Load. This function is always called when the page loads, or when the user performs a refresh. Here, the function loads the guestbook.xml file, and then the XslTranform class is used to transform the XML elements into HTML before we load the guestbook.xsl with the help of a XslTransform object.
Next, we create a new object of class XmlNodeList, which will allow us to select the required nodes. We then use the class MemoryStream, available via the namespace System.IO, to create a stream that has memory as a backing store, and use the Transform function to assign the xml data to this memory stream. The Seek function sets the current position to zero.
We then create an object of the class StreamReader, which reads the stream, and print the result with the help of the function ReadToEnd(). This function reads the stream from the current position to the end. If you run viewguestbook.aspx, you should see a Web form like this:

The XSL
As I've already mentioned, we use XSL for the transformation of the data from XML to HTML. I've assumed that you're already experienced with XSLT, so I'll only touch on the important aspects here. I have used an XSL for-each loop to iterate through all the guests in the book, which looks something like this:
<xsl:for-each select="//guest">
<xsl:apply-templates select="name"/>
</xsl:for-each>
And in the loop we call the XSL template name, which looks something like this:
<xsl:template match="name">
<xsl:value-of select='.'/>
</xsl:template>
Conclusion
As you see, it's not very difficult to create a guestbook. Good luck! And don't forget to download the example files here.