Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - XML - Creating Simple Guestbook with XML in ASP.NET and C#
Creating Simple Guestbook with XML in ASP.NET and C#


ASP.NET XML Tutorial

This tutorial instructs how to create a simple Guestbook using an XML file for storage of comments. C# version.

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

User interaction on a website is becoming more and more common; allowing users to add comments on a website is one of many features we can implement to increase interactivity on a site. Such interactivity as a guestbook can use either XML or a database as a storage medium for comments. In this tutorial, we will show how we can use an XML file to store guestbook entries, and add new ones to it.
Firstly, we will need the following assembly reference:

using System.XML;

We add a small form to allow new entries, and also a DataList to display existing entries
The ASPX page will look something like this:

<form id="form1" runat="server">
 <table>
<tr>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Location:</td>
<td style="width: 100px">
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Email:</td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Comments:</td>
<td style="width: 100px">
<asp:TextBox ID="txtComments" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></td>
</tr>
</table>
<br />
<asp:DataList ID="Guestbook" Runat="server" Width="100%">
<ItemTemplate>
<hr size=0/>
Name: <%# DataBinder.Eval(Container.DataItem, "name") %><br />
E-mail: <a href="mailto:<%# DataBinder.Eval(Container.DataItem, "email") %>"><%# DataBinder.Eval(Container.DataItem, "email") %></a><br />
Location: <%# DataBinder.Eval(Container.DataItem, "location") %><br />
Date: <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<i><%# DataBinder.Eval(Container.DataItem, "entry_Text") %></i>
</ItemTemplate>
</asp:DataList>
</form>

Our XML document will look something like this, with the first guestbook entry:

<?xml version="1.0" encoding="utf-8"?>
<guestbook>
<entry name="Fred Bloggs" email="tutorials@aspnettutorials.com" location="Florida" date="04/29/2008 4:30:30 PM">This is the first entry !</entry>
</guestbook>

The code-behind will look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}

protected void Button1_Click(object sender, EventArgs e)
{
// Open the XML doc
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath("guestbook.xml"));
System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

// Create new XML element and populate its attributes
System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
myXmlElement.SetAttribute("name", Server.HtmlEncode(txtName.Text));
myXmlElement.SetAttribute("email", Server.HtmlEncode(txtEmail.Text));
myXmlElement.SetAttribute("location", Server.HtmlEncode(txtLocation.Text));
myXmlElement.SetAttribute("date", DateTime.Now.ToString());
myXmlElement.InnerText = Server.HtmlEncode(txtComments.Text);

// Insert data into the XML doc and save
myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
myXmlDocument.Save(Server.MapPath("guestbook.xml"));

// Re-bind data since the doc has been added to
BindData();
}

void BindData()
{
XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("guestbook.xml"));
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();

Guestbook.DataSource = myDataSet.Tables[0];
Guestbook.DataBind();
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks