Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


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 &quot;System.Xml.XmlDocument&quot; and the &quot;System.Xml.XmlNavigator&quot; 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 &quot;Request.Params[]&quot; to get the data posted in a query string calling the page.<br> <br> 3) <i> viewguestbook.aspx</i> - It uses the Class &quot;System.Xml.XmlDataDocument&quot; to get the viewer data from the XML file into a &quot;System.Data.DataSet&quot; 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 &quot;Repeater&quot; to display the data in the DataSet.&nbsp;</p> <p><span class=wboxheado>Code</span><br> 1) <i><b>guestpost.aspx</b> :- The Guestbook post page&nbsp;</i></p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;%@ Page Language=&quot;C#&quot; EnableSessionState=&quot;False&quot; Debug=&quot;True&quot; %&gt; &lt;%@ Import Namespace=&quot;System.IO&quot; %&gt; &lt;%@ Assembly Name=&quot;System.Xml&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Xml&quot; %&gt; <span class=cmt>&lt;%-- These are the imported assemblies and namespaces need to run the guest book --%&gt;</span> &lt;html&gt; &lt;head&gt; &lt;title&gt;Welcome to Saurabh's GuestBook.&lt;/title&gt; &lt;script Language=&quot;C#&quot; runat=&quot;server&quot;&gt; <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 = &quot;db/guest.xml&quot; ; <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&amp;&amp;Name.Text!=&quot;&quot;&amp;&amp;Country.Text!=&quot;&quot;&amp;&amp;Email.Text!=&quot;&quot;){ errmess.Text=&quot;&quot; ; <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 &quot;FileShare.ReadWrite&quot; 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, &quot;Guest&quot;,&quot;&quot;,&quot;&quot;) ; <span class=cmt>//Insert the Element of Name as the First node of 'Guest'</span> navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, &quot;Name&quot;,&quot;&quot;,&quot;&quot;) ; <span class=cmt>//This is important to specify what kind of Value will the //Name element contain</span> navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, &quot;Name&quot;,&quot;&quot;,&quot;&quot;) ; <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, &quot;Country&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, &quot;Country&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Value=Country.Text ; navigator.MoveToParent() ; navigator.Insert(TreePosition.After,XmlNodeType.Element, &quot;Email&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, &quot;Email&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Value=Email.Text; navigator.MoveToParent() ; navigator.Insert(TreePosition.After,XmlNodeType.Element, &quot;Comments&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, &quot;comments&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Value=Comments.Value ; navigator.MoveToParent() ; navigator.Insert(TreePosition.After, XmlNodeType.Element, &quot;DateTime&quot;,&quot;&quot;,&quot;&quot;) ; navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text, &quot;DateTime&quot;,&quot;&quot;,&quot;&quot;) ; <span class=cmt>//set the Date time stamp of the entry</span> DateTime now = DateTime.Now ; navigator.Value=now.ToShortDateString()+&quot; &quot; +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=&quot;Name=&quot; +System.Web.HttpUtility.UrlEncodeToString(Name.Text, System.Text.Encoding.UTF8); QueryString+=&quot;&amp;&amp;Country=&quot; +System.Web.HttpUtility.UrlEncodeToString(Country.Text, System.Text.Encoding.UTF8); QueryString+=&quot;&amp;&amp;Email=&quot; +System.Web.HttpUtility.UrlEncodeToString(Email.Text, System.Text.Encoding.UTF8); QueryString+=&quot;&amp;&amp;Comments=&quot; +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(&quot;viewPost.aspx?&quot; + QueryString); } else { <span class=cmt>//if any of the Fields are kept empty show an error message</span> errmess.Text=&quot;Fill in all the required fields of the Guestbook.&quot;; } } catch (Exception edd) { <span class=cmt>//catch any other exception that occur </span> errmess.Text=&quot;Cannot write to XML file because &quot;+edd.ToString(); } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt; &lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot;&gt; <span class=cmt>&lt;%-- Include a header file 'header.inc' --%&gt;</span> &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;br&gt; &lt;h3 align=&quot;center&quot; class=&quot;newsbody&quot;&gt;Guestbook Post Page.&lt;/h3&gt; &lt;br&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;color:#FF0000&quot; runat=&quot;server&quot; /&gt; &lt;form runat=&quot;server&quot;&gt; &lt;table border=&quot;0&quot; width=&quot;80%&quot; align=&quot;Center&quot;&gt; &lt;tr &gt; &lt;td class=&quot;newsheading&quot;&gt;&lt;b&gt;Sign-in My GuestBook&lt;/b&gt;&lt;/td&gt; &lt;td class=&quot;newsheading&quot;&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot; &gt; &lt;td&gt;Name :&lt;/td&gt; &lt;td &gt;&lt;asp:textbox text=&quot;&quot; id=&quot;Name&quot; runat=&quot;server&quot; /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#FF0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;Country :&lt;/td&gt; &lt;td&gt;&lt;asp:textbox text=&quot;&quot; id=&quot;Country&quot; runat=&quot;server&quot;/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#FF0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;E-Mail :&lt;/td&gt; &lt;td&gt;&lt;asp:textbox test=&quot;&quot; id=&quot;Email&quot; runat=&quot;server&quot;/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#FF0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;Comments :&lt;/td&gt; &lt;td&gt;&lt;textarea id=&quot;Comments&quot; cols=&quot;25&quot; rows=&quot;4&quot; runat=&quot;server&quot; /&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td colspan=&quot;2&quot; &gt; &lt;asp:Button class=&quot;newsheading&quot; Text=&quot;Submit&quot; onClick=&quot;Submit_Click&quot; runat=&quot;server&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;br&gt; &lt;h4 class=&quot;newsbody&quot;&gt;&lt;a href=&quot;viewguestbook.aspx&quot;&gt;Click here &lt;/a&gt; to view GuestBook.&lt;/h4&gt; &lt;br&gt; &lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt; &lt;/html&gt;</pre> </td> </tr> </table><P>&nbsp;</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>&lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;%@ Page Language=&quot;C#&quot; Debug=&quot;true&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Welcome to Saurabh's GuestBook.&lt;/title&gt; &lt;script language=&quot;C#&quot; runat=&quot;server&quot; &gt; <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[&quot;Name&quot;]; CountryLabel.Text= Request.Params[&quot;Country&quot;] ; EmailLabel.Text=Request.Params[&quot;Email&quot;]; CommentsLabel.Text=Request.Params[&quot;Comments&quot;] ; } if(Page.IsPostBack) { <span class=cmt>//else display an error</span> errmess.Text=&quot;This Page Cannot be called directly. &quot; ; } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt; &lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot; &gt; &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;color:#FF0000&quot; runat=&quot;server&quot; /&gt; &lt;center&gt; &lt;h2 class=&quot;newsbody&quot;&gt;&lt;b&gt;Thank You, for posting in My GuestBook.&lt;/b&gt;&lt;/h2&gt; &lt;table align=center width=&quot;60%&quot; border=&quot;0&quot; cellspacing=&quot;2&quot; cellpadding=&quot;1&quot; &gt; &lt;tr class=&quot;titheading&quot;&gt;&lt;td colspan=&quot;2&quot;&gt;The information You Posted! &lt;/td&gt;&lt;/tr&gt;&lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;Name :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;NameLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;Country :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;CountryLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;E-mail :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;EmailLabel&quot; text=&quot;&quot; runat=&quot;server&quot;/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;newsbody&quot;&gt; &lt;td&gt;Comments :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;CommentsLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;h4 class=&quot;newsbody&quot;&gt;&lt;a href=&quot;viewguestbook.aspx&quot;&gt;Click here &lt;/a&gt; to view GuestBook.&lt;/h4&gt; &lt;br&gt; &lt;/center&gt; &lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt; &lt;/html&gt;</pre> </td> </tr> </table><P>&nbsp;</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>&lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;%@ Import Namespace=&quot;System.IO&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data&quot; %&gt; &lt;%@ Assembly Name=&quot;System.Xml&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Xml&quot; %&gt; &lt;%@ Page Language=&quot;C#&quot; %&gt; <span class=cmt>&lt;%-- Needed Assemblies --%&gt;</span> &lt;html&gt; &lt;head&gt; &lt;title&gt;Welcome to Saurabh's GuestBook.&lt;/title&gt; &lt;script language=&quot;C#&quot; runat=server&gt; <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 = &quot;db/guest.xml&quot; ; <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=&quot;Cannot read from XML file because &quot;+edd.ToString() ; } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt; &lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginwidth=&quot;0&quot;&gt; &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;color:#FF0000&quot; runat=&quot;server&quot; /&gt; &lt;br&gt; &lt;h3 align=&quot;center&quot; class=&quot;newsbody&quot;&gt;My Guestbook.&lt;/h3&gt; &lt;ASP:Repeater id=&quot;MyDataList&quot; runat=&quot;server&quot;&gt; &lt;template name=&quot;headertemplate&quot;&gt; &lt;table class=&quot;mainheads&quot; width=&quot;100%&quot; style=&quot;font: 8pt verdana&quot;&gt; &lt;tr style=&quot;background-color:#FF9966&quot;&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; Country &lt;/th&gt; &lt;th&gt; Email &lt;/th&gt; &lt;th&gt; Comments &lt;/th&gt; &lt;th&gt; Date/Time &lt;/th&gt; &lt;/tr&gt; &lt;/template&gt; &lt;template name=&quot;itemtemplate&quot;&gt; &lt;tr style=&quot;background-color:#FFFFCC&quot;&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, &quot;Name&quot;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, &quot;Country&quot;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, &quot;Email&quot;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, &quot;Comments&quot;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, &quot;DateTime&quot;) %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/template&gt; &lt;template name=&quot;footertemplate&quot;&gt; &lt;/table&gt; &lt;/template&gt; &lt;/ASP:Repeater&gt; &lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt; &lt;/html&gt;</pre> </td> </tr> </table>

Comments

Add Comment