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 xml
(标准化越来越近了)Document
xml
(标准化越来越近了)Document xml
(标准化越来越近了)document = new xml
(标准化越来越近了)Document() ;
//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) ;
xml
(标准化越来越近了)document.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(xml
(标准化越来越近了)document) ;
//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, xml
(标准化越来越近了)NodeType.Element, "Guest","","") ;
//Insert the Element of Name as the First node of 'Guest'
navigator.Insert(TreePosition.FirstChild, xml
(标准化越来越近了)NodeType.Element, "Name","","") ;
//This is important to specify what kind of Value will the Name element contain
navigator.Insert(TreePosition.FirstChild, xml
(标准化越来越近了)NodeType.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, xml
(标准化越来越近了)NodeType.Element,"Country","","") ;
navigator.Insert(TreePosition.FirstChild, xml
(标准化越来越近了)NodeType.Text,"Country","","") ;
navigator.Value=Country.Text ;
navigator.MoveToParent() ;
navigator.Insert(TreePosition.After,xml
(标准化越来越近了)NodeType.Element,"Email","","") ;
[1] [2] [3] [4] [5] 下一页