Code:
1) guestpost.aspx :- The Guestbook post page
<%@ Import Namespace="System" %>
<%@ Page Language="C#" EnableSessionState="False" Debug="True" %>
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ 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
[1] [2] [3] 下一页