ASP.NET: XML留言版第二版

Code:

  1. guestpost.aspx :- The Guestbook post page
1@ Import Namespace="System" 
1@ Page Language="C#" EnableSessionState="False" Debug="True" 
1@ Import Namespace="System.IO" 
1@ Assembly Name="System.Xml" 
1@ Import Namespace="System.Xml" 
1-- These are the imported assemblies and namespaces need to run the guest book --
  1<html>
  2<head>
  3<title>Welcome to Saurabh's GuestBook.</title>
  4<script language="C#" runat="server">   
  5//This method is called when the submit button is clicked   
  6public void Submit_Click(Object sender, EventArgs e)   
  7{   
  8//the path to the Xml file which will contain all the data   
  9//modify this if you have any other file or directory mappings.   
 10//modify this if you have been directed here from Step 2 of the ReadMe file.   
 11string datafile = "db/guest.xml" ;   
 12//put the posting code within a Try-Catch block   
 13try   
 14{   
 15//proceed only if all the required feilds are filled-in   
 16if(Page.IsValid&&Name.Text!=""&&Country.Text!=""&&Email.Text!=""){   
 17errmess.Text="" ;   
 18//make an instance of the class XmlDocument   
 19XmlDocument xmldocument = new XmlDocument() ;   
 20//load the xml file you will use as your database.   
 21//Since we are working on a server we have to use 'Server.MapPath()'   
 22//to map the path to the database file //Also Open a FileStream to the Database   
 23//Keep "FileShare.ReadWrite" mode to enable sharing   
 24FileStream fin ;   
 25fin = new FileStream(Server.MapPath(datafile), FileMode.Open,   
 26FileAccess.Read, FileShare.ReadWrite) ;   
 27xmldocument.Load(new StreamReader(fin)) ;   
 28fin.Close();   
 29//make an instance of DocumentNavigator class which will help us to   
 30//navigate in the loaded XML data file.   
 31DocumentNavigator navigator = new DocumentNavigator(xmldocument) ;   
 32  
 33//below code is very significant as it navigates through the XML document and   
 34//stores the required values (ps: read this code carefully)   
 35//first move to the xml documents elements   
 36//(in my xml file this will be the 'Guests' Node)   
 37navigator.MoveToDocumentElement() ;   
 38//then insert First element (FirstChild) which will contain all the information   
 39// of a single guest posting   
 40navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Guest","","") ;   
 41//Insert the Element of Name as the First node of 'Guest'   
 42navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Name","","") ;   
 43//This is important to specify what kind of Value will the Name element contain   
 44navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Name","","") ;   
 45//assign the Name element the Value from the .Text property of the TextBox   
 46navigator.Value=Name.Text ;   
 47//Go back to the Parent Node ie 'Guest'   
 48navigator.MoveToParent() ;   
 49//Insert another Element 'Country' After the FirstChild ie. after the 'Name' node.   
 50navigator.Insert(TreePosition.After, XmlNodeType.Element,"Country","","") ;   
 51navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Country","","") ;   
 52navigator.Value=Country.Text ;   
 53navigator.MoveToParent() ;   
 54navigator.Insert(TreePosition.After,XmlNodeType.Element,"Email","","") ;   
 55navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Email","","") ;   
 56navigator.Value=Email.Text;   
 57  
 58navigator.MoveToParent() ;   
 59navigator.Insert(TreePosition.After,XmlNodeType.Element,"Comments","","") ;   
 60navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"comments","","") ;   
 61navigator.Value=Comments.Value ;   
 62navigator.MoveToParent() ;   
 63  
 64navigator.Insert(TreePosition.After, XmlNodeType.Element,"DateTime","","") ;   
 65navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"DateTime","","") ;   
 66//set the Date time stamp of the entry   
 67DateTime now = DateTime.Now ;   
 68navigator.Value=now.ToShortDateString()+" "+now.ToShortTimeString() ;   
 69  
 70//Create a stream to save the file   
 71FileStream fout ;   
 72fout = new FileStream(Server.MapPath(datafile), FileMode.Open,   
 73FileAccess.Write, FileShare.ReadWrite) ;   
 74//after making the necessary changes we Save the changes to the Xml Document   
 75xmldocument.Save(new StreamWriter(fout)) ;   
 76//free up the XML file from the Document file so that other programs can use it   
 77xmldocument=null ;   
 78fout.Close();   
 79//Build a custom query sending the data posted to another page for display   
 80//since it is a query we have to encode it in UTF8 format   
 81String QueryString="Name="   
 82+System.Web.HttpUtility.UrlEncodeToString(Name.Text,System.Text.Encoding.UTF8);   
 83QueryString+="&&Country="   
 84+System.Web.HttpUtility.UrlEncodeToString(Country.Text,System.Text.Encoding.UTF8);   
 85QueryString+="&&Email="   
 86+System.Web.HttpUtility.UrlEncodeToString(Email.Text,System.Text.Encoding.UTF8);   
 87QueryString+="&&Comments="   
 88+System.Web.HttpUtility.UrlEncodeToString(Comments.Value,System.Text.Encoding.UTF8);   
 89  
 90//go to the page viewpost.aspx and append the query string at its end.   
 91Page.Navigate("viewPost.aspx?" + QueryString);   
 92  
 93}   
 94else   
 95{   
 96//if any of the Fields are kept empty show an error message   
 97errmess.Text="Fill in all the required fields of the Guestbook." ;   
 98}   
 99}   
100catch (Exception edd)   
101{   
102//catch any other exception that occur   
103errmess.Text="Cannot write to XML file because "+edd.ToString() ;   
104}   
105}   
106</script>
107<link href="mystyle.css" rel="stylesheet" type="text/css"/>
108</head>
109<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">   

-- Include a header file 'header.inc' --

 1<!-- #Include File="header.inc" -->
 2<br/>
 3<h3 align="center" class="newsbody">Guestbook Post Page.</h3>
 4<br/>
 5<asp:label id="errmess" runat="server" style="color:#FF0000" text=""></asp:label>
 6<form runat="server">
 7<table align="Center" border="0" width="80%">
 8<tr>
 9<td class="newsheading"><b>Sign-in My GuestBook</b></td>
10<td class="newsheading"> </td>
11</tr>
12<tr class="newsbody">
13<td>Name :</td>
14<td><asp:textbox id="Name" runat="server" text=""></asp:textbox>      
15<font color="#FF0000">*</font></td>
16</tr>
17<tr class="newsbody">
18<td>Country :</td>
19<td><asp:textbox id="Country" runat="server" text=""></asp:textbox>      
20<font color="#FF0000">*</font></td>
21</tr>
22<tr class="newsbody">
23<td>E-Mail :</td>
24<td><asp:textbox id="Email" runat="server" test=""></asp:textbox>      
25<font color="#FF0000">*</font></td>
26</tr>
27<tr class="newsbody">
28<td>Comments :</td>
29<td><textarea cols="25" id="Comments" rows="4" runat="server"></textarea></td>
30</tr>
31<tr class="newsbody">
32<td colspan="2">
33<asp:button class="newsheading" onclick="Submit_Click" runat="server" text="Submit"></asp:button>
34</td>
35</tr>
36</table>
37</form>
38<br/>
39<h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4>
40<br/>
41<!-- #Include File="footer.inc" -->
42</body>   
43/html&gt;   
44  
45  
46  
47  
482) viewpost.aspx : The post conformation page.   
49  

@ Import Namespace="System"

@ Page Language="C#" Debug="true"

 1<html>
 2<head>
 3<title>Welcome to Saurabh's GuestBook.</title>
 4<script language="C#" runat="server">   
 5//execute this script when the page loads   
 6void Page_Load(Object Src, EventArgs E)   
 7{   
 8//if the page is called from another page   
 9if (!Page.IsPostBack) {   
10//get the different Parameters from the query string and store it   
11//to respective Labels   
12NameLabel.Text = Request.Params["Name"];   
13CountryLabel.Text= Request.Params["Country"] ;   
14EmailLabel.Text=Request.Params["Email"];   
15CommentsLabel.Text=Request.Params["Comments"] ;   
16}   
17if(Page.IsPostBack)   
18{   
19//else display an error   
20errmess.Text="This Page Cannot be called directly. " ;   
21}   
22}   
23</script>
24<link href="mystyle.css" rel="stylesheet" type="text/css"/>
25</head>
26<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
27<!-- #Include File="header.inc" -->
28<asp:label id="errmess" runat="server" style="color:#FF0000" text=""></asp:label>
29<center>
30<h2 class="newsbody"><b>Thank You , for posting in My GuestBook.</b></h2>
31<table align="center" border="0" cellpadding="1" cellspacing="2" width="60%">
32<tr class="titheading"><td colspan="2">The information You Posted!</td></tr>
33<tr class="newsbody">
34<td>Name :</td>
35<td><asp:label id="NameLabel" runat="server" text=""></asp:label></td>
36</tr>
37<tr class="newsbody">
38<td>Country :</td>
39<td><asp:label id="CountryLabel" runat="server" text=""></asp:label></td>
40</tr>
41<tr class="newsbody">
42<td>E-mail :</td>
43<td><asp:label id="EmailLabel" runat="server" text=""></asp:label></td>
44</tr>
45<tr class="newsbody">
46<td>Comments :</td>
47<td><asp:label id="CommentsLabel" runat="server" text=""></asp:label></td>
48</tr>
49</table>
50<br/>
51<h4 class="newsbody"><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4>
52<br/>
53</center>
54<!-- #Include File="footer.inc" -->
55</body>
56</html>   
57  
58  
59  
60  
612) viewguestbook.aspx : The Guestbook viewing page.   
62  

@ Import Namespace="System"

@ Import Namespace="System.IO"

@ Import Namespace="System.Data"

@ Assembly Name="System.Xml"

@ Import Namespace="System.Xml"

@ Page Language="C#"

-- Needed Assembiles --

 1<html>
 2<head>
 3<title>Welcome to Saurabh's GuestBook.</title>
 4<script language="C#" runat="server">   
 5//run the script when the Page is Loaded   
 6public void Page_Load(Object sender, EventArgs e)   
 7{   
 8//the path to the Xml file which will contain all the data   
 9//modify this if you have any other file or directory mappings.   
10//modify this if you have been directed here from Step 2 of the ReadMe file.   
11string datafile = "db/guest.xml" ;   
12//try-Catch block to read from an XML file   
13try   
14{   
15//make an instance to the XMLDataDocument class   
16//this class can read from an xml file in and ordered format   
17XmlDataDocument datadoc = new XmlDataDocument();   
18//Open a FileStream to the Database   
19FileStream fin ;   
20fin = new FileStream(Server.MapPath(datafile),FileMode.Open,   
21FileAccess.Read,FileShare.ReadWrite) ;   
22// Infer the DataSet schema from the XML data and load the XML Data   
23datadoc.DataSet.ReadXml(new StreamReader(fin));   
24//Databind the first table in the Dataset to the Repeater   
25MyDataList.DataSource = datadoc.DataSet.Tables[0].DefaultView;   
26MyDataList.DataBind();   
27  
28//free up the XML file to be used by other programs   
29datadoc=null;   
30  
31}   
32catch (Exception edd)   
33{   
34//catch any other exceptions that occur   
35errmess.Text="Cannot read from XML file because "+edd.ToString() ;   
36}   
37}   
38</script>
39<link href="mystyle.css" rel="stylesheet" type="text/css"/>
40</head>
41<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
42<!-- #Include File="header.inc" -->
43<asp:label id="errmess" runat="server" style="color:#FF0000" text=""></asp:label>
44<br/>
45<h3 align="center" class="newsbody">My Guestbook.</h3>
46<asp:repeater id="MyDataList" runat="server">
47<template name="headertemplate">
48<table class="mainheads" style="font: 8pt verdana" width="100%">
49<tr style="background-color:#FF9966">
50<th>   
51Name   
52</th>
53<th>   
54Country   
55</th>
56<th>   
57Email   
58</th>
59<th>   
60Comments   
61</th>
62<th>   
63Date/Time   
64</th>
65</tr>
66</table></template>
67<template name="itemtemplate">
68<tr style="background-color:#FFFFCC">
69<td>   

DataBinder.Eval(Container.DataItem, "Name")

1</td>
2<td>   

DataBinder.Eval(Container.DataItem, "Country")

1</td>
2<td>   

DataBinder.Eval(Container.DataItem, "Email")

1</td>
2<td>   

DataBinder.Eval(Container.DataItem, "Comments")

1</td>
2<td>   

DataBinder.Eval(Container.DataItem, "DateTime")

 1</td>
 2</tr>
 3</template>
 4<template name="footertemplate">
 5
 6</template>
 7</asp:repeater>
 8<!-- #Include File="footer.inc" -->
 9</body>
10</html>   
11  
12  
13Saurabh Nandu</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus