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> </form> <tr> <asp:TextBox ID="txtName" runat="server"></asp:TextBox></td> <td style="width: 100px"> Name:</td> <td style="width: 100px"></tr> </table><tr> <td style="width: 100px"> </tr>Location:</td> <td style="width: 100px"> <asp:TextBox ID="txtLocation" runat="server"></asp:TextBox></td> <tr> <td style="width: 100px"> </tr>Email:</td> <td style="width: 100px"> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td> <tr> <td style="width: 100px"> </tr>Comments:</td> <td style="width: 100px"> <asp:TextBox ID="txtComments" runat="server"></asp:TextBox></td> <tr> <td colspan="2"> </tr><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></td> <br /> <asp:DataList ID="Guestbook" Runat="server" Width="100%"> <ItemTemplate> </asp:DataList><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> |
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 End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load BindData() End SubProtected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) ' Open the XML doc End SubDim 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() Private Sub BindData() Dim myXmlReader As New XmlTextReader(Server.MapPath("guestbook.xml")) End SubDim myDataSet As New DataSet() myDataSet.ReadXml(myXmlReader) myXmlReader.Close() Guestbook.DataSource = myDataSet.Tables(0) Guestbook.DataBind() |
No comments:
Post a Comment