Introduction
This article will basically guide you on how to create a simple GuestBook for your website with the help of XML. Lots of guestbook that you can found on the internet basically utilise Database to store the backend data. In fact, for guestbook, you actually don't need to store all the data inside the database. You can just use XML file for storing the Data. XML is lightweight and you can format the look and feel easily using XSLT and you can also open it direct with Notepad. You don't have to worry about the connection string especially if you migrate your website to different hosting provider.
Building ASP.NET GuestBook with XML Backend
To Start building GuestBook for your website, lets open Visual Studio 2005 and Create two pages called
GuestBook.aspx, and ShowGuestBook.aspx.
The GuestBook.aspx will be a page where your user can fill in their data and submit to your website and ShowGuestBook.aspx will be a page that display current available guest on your website.
Both pages will be retrieving and saving the data into the XML File. No database needed.
Let's Start code something for GuestBook.aspx
<TABLE WIDTH="600">
<TR>
<TD>
Your Name</TD>
<TD>
<ASP:TEXTBOX ID="txtName" RUNAT="server" COLUMNS="30" MAXLENGTH="30" /></TD>
</TR>
<TR>
<TD>
Email Address</TD>
<TD>
<ASP:TEXTBOX ID="txtEmail" RUNAT="server" COLUMNS="50" MAXLENGTH="50" /></TD>
</TR>
<TR>
<TD VALIGN="top">
Your Comments</TD>
<TD>
<ASP:TEXTBOX ID="txtComments" RUNAT="server" COLUMNS="30" ROWS="4" TEXTMODE="multiline"
WRAP="true" /></TD>
</TR>
<TR>
<TD COLSPAN="2" ALIGN="right" style="height: 26px">
<ASP:BUTTON ID="btnSubmit" TEXT="Submit" ONCLICK="btnSubmit_Click" RUNAT="server" /></TD>
</TR>
</TABLE>
<BR />
<A HREF='ShowGuestBook.aspx'>View GuestBook</A>The Page should be displayed like this

For this page, we need to handle the Submit Button Click Event. And the OnClick Event basically will save the current data into XML File. For this, we will be using DataSet objects to manipulate and save the XML File. There is a method called WriteXML from the DataSet objects that we can use to achieve this functionality. There is also another subset classes from .NET Framework that specifically written for XML manipulation, and all the classes is inside the System.XML objects. However I choose to use DataSet objects because it could represent XML data as a relational information as if you are working on a real relational database.
Submit Button Code Behind (GuestBook.aspx)
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!File.Exists(Server.MapPath("XML/GuestBook.xml")))
{
CreateXMLFile();
}
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XML/GuestBook.xml"), XmlReadMode.ReadSchema);
DataRow dr = ds.Tables[0].NewRow();
dr["datetime"] = DateTime.Now;
dr["name"] = txtName.Text.ToString();
dr["email"] = txtEmail.Text.ToString();
dr["comments"] = txtComments.Text.ToString();
ds.Tables[0].Rows.Add(dr);
//Write to XML
ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);
txtName.Text = "";
txtEmail.Text = "";
txtComments.Text = "";
}
CreateXML Method (GuestBook.aspx)
private void CreateXMLFile()
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("GuestBook");
DataColumn dc = dt.Columns.Add("id", Type.GetType("System.Int32"));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dt.Columns.Add("datetime", Type.GetType("System.DateTime"));
dt.Columns.Add("name", Type.GetType("System.String"));
dt.Columns.Add("email", Type.GetType("System.String"));
dt.Columns.Add("comments", Type.GetType("System.String"));
ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);
}
As you can see from the code above, we just use the DataSet WriteXML Method to write to the XML Files.
We set the Id to be autoincrement, everytime the user sign the guestbook
Now, for the ShowGuestBook.aspx, the code is basically pretty simple. We use Repeater to display the data from the XML File.
Below is the code snippet
ShowGuestBook.aspx<FORM ID="form1" RUNAT="server">
<ASP:REPEATER ID="myRepeater" RUNAT="server">
<HEADERTEMPLATE>
<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0" WIDTH="100%" STYLE="font: 10pt verdana">
<TR>
<TD><h2>My Guest Book</h2></TD>
</TR>
</HEADERTEMPLATE>
<ITEMTEMPLATE>
<TR>
<TD WIDTH="100%">
Name :
<%# DataBinder.Eval(Container.DataItem, "name") %>
<BR />
Email :
<%# DataBinder.Eval(Container.DataItem, "email") %>
<BR />
Comments :
<%# DataBinder.Eval(Container.DataItem, "comments") %>
<BR />
Date :
<%# DataBinder.Eval(Container.DataItem, "datetime") %>
<BR /><BR />
<HR />
</TD>
</TR>
</ITEMTEMPLATE>
<FOOTERTEMPLATE>
</table>
</FOOTERTEMPLATE>
</ASP:REPEATER>
</FORM>
Code for Binding the Repeater (ShowGuestBook.aspx)
private void BindData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XML/GuestBook.xml"),XmlReadMode.ReadSchema);
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = "id desc";
myRepeater.DataSource = dv;
myRepeater.DataBind();
}
Conclusion
That's all you need basically to create a simple GuestBook that writes and read from XML file. As our code is written using DataSet. You can actually easily change the code so that it reads from Access Database or MSSQL Database. It is your choice. If you have large user base, it might be good idea to store it into database.
Download Source Code
Other Related and Popular Articles :
Author Profile : James Douglas
 | I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course. I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use. I love .NET programming and am doing it almost every day now.Click here to view Author Profile |
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
James Douglas
Everything else Copyright © by
WorldofASP.NET
2008