Wednesday, 27 July 2011

Creating Simple Guestbook with XML in ASP.NET and VB


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

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:

Imports System.Data
Imports System.Xml

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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>

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

The code-behind will look something like this:

Imports System.Data
Imports System.Xml

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindData()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
' Open the XML doc
Dim myXmlDocument As New System.Xml.XmlDocument()
myXmlDocument.Load(Server.MapPath("guestbook.xml"))
Dim myXmlNode As System.Xml.XmlNode = myXmlDocument.DocumentElement.FirstChild

' Create new XML element and populate its attributes
Dim myXmlElement As System.Xml.XmlElement = 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()
End Sub

Private Sub BindData()
Dim myXmlReader As New XmlTextReader(Server.MapPath("guestbook.xml"))
Dim myDataSet As New DataSet()
myDataSet.ReadXml(myXmlReader)
myXmlReader.Close()

Guestbook.DataSource = myDataSet.Tables(0)
Guestbook.DataBind()
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment