ASP.NET:XML留言版第二版

网络整理 - 09-16
Code:
1) guestpost.aspx :- The Guestbook post page

<%@ Import Namespace="System" %>
<%@ Page Language="C#" EnableSessionState="False" Debug="True" %>
<%@ Import Namespace="System.IO" %>
<%@ Assembly %>
<%@ Import Namespace="System.Xml" %>
<%-- These are the imported assemblies and namespaces need to run the guest book --%>
<html>
<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script Language="C#" runat="server">
//This method is called when the submit button is clicked
public void Submit_Click(Object sender, EventArgs e)
{
//the path to the Xml file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile = "db/guest.xml" ;
//put the posting code within a Try-Catch block
try
{
//proceed only if all the required feilds are filled-in
if(Page.IsValid&&Name.Text!=""&&Country.Text!=""&&Email.Text!=""){
errmess.Text="" ;
//make an instance of the class XmlDocument
XmlDocument xmldocument = new XmlDocument() ;
//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
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile), FileMode.Open,
FileAccess.Read, FileShare.ReadWrite) ;
xmldocument.Load(new StreamReader(fin)) ;
fin.Close();
//make an instance of DocumentNavigator class which will help us to
//navigate in the loaded XML data file.
DocumentNavigator navigator = new DocumentNavigator(xmldocument) ;

//below code is very significant as it navigates through the XML document and
//stores the required values (ps: read this code carefully)
//first move to the xml documents elements
//(in my xml file this will be the 'Guests' Node)
navigator.MoveToDocumentElement() ;
//then insert First element (FirstChild) which will contain all the information
// of a single guest posting
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Guest","","") ;
//Insert the Element of Name as the First node of 'Guest'
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element, "Name","","") ;
//This is important to specify what kind of Value will the Name element contain
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Name","","") ;
//assign the Name element the Value from the .Text property of the TextBox
navigator.Value=Name.Text ;
//Go back to the Parent Node ie 'Guest'
navigator.MoveToParent() ;
//Insert another Element 'Country' After the FirstChild ie. after the 'Name' node.
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","","") ;
//set the Date time stamp of the entry
DateTime now = DateTime.Now ;
navigator.Value=now.ToShortDateString()+" "+now.ToShortTimeString() ;

//Create a stream to save the file
FileStream fout ;
fout = new FileStream(Server.MapPath(datafile), FileMode.Open,
FileAccess.Write, FileShare.ReadWrite) ;
//after making the necessary changes we Save the changes to the Xml Document
xmldocument.Save(new StreamWriter(fout)) ;
//free up the XML file from the Document file so that other programs can use it
xmldocument=null ;
fout.Close();
//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
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);

//go to the page viewpost.aspx and append the query string at its end.
Page.Navigate("viewPost.aspx?" + QueryString);

}
else
{
//if any of the Fields are kept empty show an error message
errmess.Text="Fill in all the required fields of the Guestbook." ;
}
}
catch (Exception edd)
{
//catch any other exception that occur
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" marginwidth="0" marginheight="0">
<%-- Include a header file 'header.inc' --%>
<!-- #Include File="header.inc" -->
<br>
<h3>Guestbook Post Page.</h3>
<br>
<asp:label text="" runat="server" />
<form runat="server">
<table>
<tr >
<td><b>Sign-in My GuestBook</b></td>
<td> </td>
</tr>
<tr >
<td>Name :</td>
<td ><asp:textbox text="" runat="server" />   
<font color=#FF0000>*</font></td>
</tr>
<tr>
<td>Country :</td>
<td><asp:textbox text="" runat="server"/>   
<font color=#FF0000>*</font></td>
</tr>
<tr>
<td>E-Mail :</td>
<td><asp:textbox test="" runat="server"/>   
<font color=#FF0000>*</font></td>
</tr>
<tr>
<td>Comments :</td>
<td><textarea cols="25" rows="4" runat="server" /></td>
</tr>
<tr>
<td colspan="2" >
<asp:Button Text="Submit" onClick="Submit_Click" runat="server"/>
</td>
</tr>
</table>
</form>
<br>
<h4><a href="viewguestbook.aspx">Click here </a> to view GuestBook.</h4>
<br>
<!-- #Include File="footer.inc" -->
</body>
/html>




2)