XML Guestbook 2
Add Comment<div align="center"> <table border="0" width="90%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a href="../../file/guestbook2.zip" class="wbox">guestbook2.zip</a> (10kb)</td> <td width="50%" class="outline">Beta1</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> Guestbook- is one of the component of a website which a web master loves to read (Including Me!!!) . So here I have a guestbook for everyone. It uses XML as its Database because all servers support XML files. Also you can customize the viewing of XML file as you like.</p> <p><span class=wboxheado>Requirements</span><br> 1) .NET SDK beta1 (Note: This example might not work on future versions of the SDK).<br> 2) ASP.NET supported web server.<br> <br> <span class=wboxheado>Explanation</span><br> This example consists of 3 parts.<br> 1) <i> guestpost.aspx</i> - Contains the code to post user information in a XML file. It uses the Class "System.Xml.XmlDocument" and the "System.Xml.XmlNavigator" to Read and Append information in a XML file.<br> XmlNavigator provides very easy ways to navigate through a XML document.<br> <br> 2) <i> viewpost.aspx</i> - This files is called from within the guestpost.aspx page to show conformation of user posted data. It uses "Request.Params[]" to get the data posted in a query string calling the page.<br> <br> 3) <i> viewguestbook.aspx</i> - It uses the Class "System.Xml.XmlDataDocument" to get the viewer data from the XML file into a "System.Data.DataSet" object. After the a DataSet object is created then it is very easy to manipulate the Data to viewing purpose. In this example we use a "Repeater" to display the data in the DataSet. </p> <p><span class=wboxheado>Code</span><br> 1) <i><b>guestpost.aspx</b> :- The Guestbook post page </i></p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre><%@ Import Namespace="System" %> <%@ Page Language="C#" EnableSessionState="False" Debug="True" %> <%@ Import Namespace="System.IO" %> <%@ Assembly Name="System.Xml" %> <%@ Import Namespace="System.Xml" %> <span class=cmt><%-- These are the imported assemblies and namespaces need to run the guest book --%></span> <html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script Language="C#" runat="server"> <span class=cmt>//This method is called when the submit button is clicked </span> public void Submit_Click(Object sender, EventArgs e) { <span class=cmt>//the path to the Xml file which will contain all the data //modify this if you have any other file or directory mappings.</span> string datafile = "db/guest.xml" ; <span class=cmt>//put the posting code within a Try-Catch block</span> try { <span class=cmt>//proceed only if all the required fields are filled-in</span> if(Page.IsValid&&Name.Text!=""&&Country.Text!=""&&Email.Text!=""){ errmess.Text="" ; <span class=cmt>//make an instance of the class XmlDocument </span> XmlDocument xmldocument = new XmlDocument() ; <span class=cmt>//load the xml file you will use as your database. //Since we are working on a server we have to use 'Server.MapPath()' //to map the path to the database file //Also Open a FileStream to the Database //Keep "FileShare.ReadWrite" mode to enable sharing </span>FileStream fin ; fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ; xmldocument.Load(new StreamReader(fin)) ; fin.Close(); <span class=cmt>//make an instance of DocumentNavigator class which will help us to //navigate in the loaded XML data file.</span> DocumentNavigator navigator = new DocumentNavigator(xmldocument) ; <span class=cmt>//below code is very significant as it navigates through the //XML document and stores the required values //first move to the xml documents elements //(in my xml file this will be the 'Guests' Node)</span> navigator.MoveToDocumentElement() ; <span class=cmt>//then insert First element (FirstChild) which will contain //all the information of a single guest posting</span> navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Guest","","") ; <span class=cmt>//Insert the Element of Name as the First node of 'Guest'</span> navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Name","","") ; <span class=cmt>//This is important to specify what kind of Value will the //Name element contain</span> navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, "Name","","") ; <span class=cmt>//assign the Name element the Value from the Text //property of the TextBox</span> navigator.Value=Name.Text ; <span class=cmt>//Go back to the Parent Node ie 'Guest'</span> navigator.MoveToParent() ; <span class=cmt>//Insert another Element 'Country' After the FirstChild ie. //after the 'Name' node.</span> navigator.Insert(TreePosition.After, XmlNodeType.Element, "Country","","") ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, "Country","","") ; navigator.Value=Country.Text ; navigator.MoveToParent() ; navigator.Insert(TreePosition.After,XmlNodeType.Element, "Email","","") ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, "Email","","") ; navigator.Value=Email.Text; navigator.MoveToParent() ; navigator.Insert(TreePosition.After,XmlNodeType.Element, "Comments","","") ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, "comments","","") ; navigator.Value=Comments.Value ; navigator.MoveToParent() ; navigator.Insert(TreePosition.After, XmlNodeType.Element, "DateTime","","") ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, "DateTime","","") ; <span class=cmt>//set the Date time stamp of the entry</span> DateTime now = DateTime.Now ; navigator.Value=now.ToShortDateString()+" " +now.ToShortTimeString() ; <span class=cmt>//Create a stream to save the file</span> FileStream fout ; fout = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Write, FileShare.ReadWrite) ; <span class=cmt> //after making the necessary changes //Save the changes to the Xml Document</span> xmldocument.Save(new StreamWriter(fout)) ; <span class=cmt>//free up the XML file from the Document file </span> xmldocument=null ; fout.Close(); <span class=cmt>//Build a custom query sending the data posted to //another page for display //since it is a query we have to encode it in UTF8 format</span> String QueryString="Name=" +System.Web.HttpUtility.UrlEncodeToString(Name.Text, System.Text.Encoding.UTF8); QueryString+="&&Country=" +System.Web.HttpUtility.UrlEncodeToString(Country.Text, System.Text.Encoding.UTF8); QueryString+="&&Email=" +System.Web.HttpUtility.UrlEncodeToString(Email.Text, System.Text.Encoding.UTF8); QueryString+="&&Comments=" +System.Web.HttpUtility.UrlEncodeToString(Comments.Value, System.Text.Encoding.UTF8); <span class=cmt>//go to the page viewpost.aspx and append the query string </span> Page.Navigate("viewPost.aspx?" + QueryString); } else { <span class=cmt>//if any of the Fields are kept empty show an error message</span> errmess.Text="Fill in all the required fields of the Guestbook."; } } catch (Exception edd) { <span class=cmt>//catch any other exception that occur </span> errmess.Text="Cannot write to XML file because "+edd.ToString(); } } </script> <LINK href="mystyle.css" type=text/css rel=stylesheet> </head> <body topmargin="0" leftmargin="0" rightmargin="0"> <span class=cmt><%-- Include a header file 'header.inc' --%></span> <!-- #Include File="header.inc" --> <br> <h3 align="center" class="newsbody">Guestbook Post Page.</h3> <br> <asp:label id="errmess" text="" style="color:#FF0000" runat="server" /> <form runat="server"> <table border="0" width="80%" align="Center"> <tr > <td class="newsheading"><b>Sign-in My GuestBook</b></td> <td class="newsheading">&nbsp;</td> </tr> <tr class="newsbody" > <td>Name :</td> <td ><asp:textbox text="" id="Name" runat="server" /> &nbsp;&nbsp;&nbsp;<font color=#FF0000>*</font></td> </tr> <tr class="newsbody"> <td>Country :</td> <td><asp:textbox text="" id="Country" runat="server"/> &nbsp;&nbsp;&nbsp;<font color=#FF0000>*</font></td> </tr> <tr class="newsbody"> <td>E-Mail :</td> <td><asp:textbox test="" id="Email" runat="server"/> &nbsp;&nbsp;&nbsp;<font color=#FF0000>*</font></td> </tr> <tr class="newsbody"> <td>Comments :</td> <td><textarea id="Comments" cols="25" rows="4" runat="server" /> </td></tr> <tr class="newsbody"> <td colspan="2" > <asp:Button class="newsheading" Text="Submit" onClick="Submit_Click" runat="server"/> </td> </tr> </table> </form> <br> <h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4> <br> <!-- #Include File="footer.inc" --> </body> </html></pre> </td> </tr> </table><P> </P> <P>2)<I> <b> viewpost.aspx</b> : The post conformation page.</I></P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre><%@ Import Namespace="System" %> <%@ Page Language="C#" Debug="true" %> <html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script language="C#" runat="server" > <span class=cmt>//execute this script when the page loads</span> void Page_Load(Object Src, EventArgs E) { <span class=cmt>//if the page is called from another page</span> if (!Page.IsPostBack) { <span class=cmt>//get the different Parameters from the query string and store it //to respective Labels</span> NameLabel.Text = Request.Params["Name"]; CountryLabel.Text= Request.Params["Country"] ; EmailLabel.Text=Request.Params["Email"]; CommentsLabel.Text=Request.Params["Comments"] ; } if(Page.IsPostBack) { <span class=cmt>//else display an error</span> errmess.Text="This Page Cannot be called directly. " ; } } </script> <LINK href="mystyle.css" type=text/css rel=stylesheet> </head> <body topmargin="0" leftmargin="0" rightmargin="0" > <!-- #Include File="header.inc" --> <asp:label id="errmess" text="" style="color:#FF0000" runat="server" /> <center> <h2 class="newsbody"><b>Thank You, for posting in My GuestBook.</b></h2> <table align=center width="60%" border="0" cellspacing="2" cellpadding="1" > <tr class="titheading"><td colspan="2">The information You Posted! </td></tr><tr class="newsbody"> <td>Name :</td> <td><asp:label id="NameLabel" text="" runat="server" /></td> </tr> <tr class="newsbody"> <td>Country :</td> <td><asp:label id="CountryLabel" text="" runat="server" /></td> </tr> <tr class="newsbody"> <td>E-mail :</td> <td><asp:label id="EmailLabel" text="" runat="server"/></td> </tr> <tr class="newsbody"> <td>Comments :</td> <td><asp:label id="CommentsLabel" text="" runat="server" /></td> </tr> </table> <br> <h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4> <br> </center> <!-- #Include File="footer.inc" --> </body> </html></pre> </td> </tr> </table><P> </P> <P>3)<I> <b> viewguestbook.aspx</b> : The Guestbook viewing page.</I></P> <table border="0" width="100%" cellspacing="0" cellpadding="0" class="code"> <tr> <td width="100%"> <pre><%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%@ Assembly Name="System.Xml" %> <%@ Import Namespace="System.Xml" %> <%@ Page Language="C#" %> <span class=cmt><%-- Needed Assemblies --%></span> <html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script language="C#" runat=server> <span class=cmt>//run the script when the Page is Loaded</span> public void Page_Load(Object sender, EventArgs e) { <span class=cmt>//the path to the Xml file which will contain all the data //modify this if you have any other file or directory mappings.</span> string datafile = "db/guest.xml" ; <span class=cmt>//try-Catch block to read from an XML file </span> try { <span class=cmt>//make an instance to the XMLDataDocument class //this class can read from an xml file in and ordered format</span> XmlDataDocument datadoc = new XmlDataDocument(); <span class=cmt>//Open a FileStream to the Database </span> FileStream fin ; fin = new FileStream(Server.MapPath(datafile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ; <span class=cmt>// Infer the DataSet schema from the XML data and load the XML Data</span> datadoc.DataSet.ReadXml(new StreamReader(fin)); <span class=cmt>//Databind the first table in the Dataset to the Repeater</span> MyDataList.DataSource = datadoc.DataSet.Tables[0].DefaultView; MyDataList.DataBind(); <span class=cmt>//free up the XML file to be used by other programs </span> datadoc=null; } catch (Exception edd) { <span class=cmt>//catch any other exceptions that occur</span> errmess.Text="Cannot read from XML file because "+edd.ToString() ; } } </script> <LINK href="mystyle.css" type=text/css rel=stylesheet> </head> <body topmargin="0" leftmargin="0" marginwidth="0"> <!-- #Include File="header.inc" --> <asp:label id="errmess" text="" style="color:#FF0000" runat="server" /> <br> <h3 align="center" class="newsbody">My Guestbook.</h3> <ASP:Repeater id="MyDataList" runat="server"> <template name="headertemplate"> <table class="mainheads" width="100%" style="font: 8pt verdana"> <tr style="background-color:#FF9966"> <th> Name </th> <th> Country </th> <th> Email </th> <th> Comments </th> <th> Date/Time </th> </tr> </template> <template name="itemtemplate"> <tr style="background-color:#FFFFCC"> <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Country") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Email") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Comments") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "DateTime") %> </td> </tr> </template> <template name="footertemplate"> </table> </template> </ASP:Repeater> <!-- #Include File="footer.inc" --> </body> </html></pre> </td> </tr> </table>