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 VB
Creating Simple Guestbook with XML in ASP.NET and VB


ASP.NET XML Tutorial

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

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

Looking for the C#.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:

Imports System.Data
Imports 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:

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

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks