当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP.NET: XML留言版第二版

ASP
用ASP编写网络传呼机
用ASP+CSS实现随机背景
ASP下载系统防盗链方法
用ASP编写下载网页中所有资源的程序
Request.ServerVariables应用
解决Asp程序的Server.CreateObject错误
ASP实现TCP端口扫描的方法
源码实例:ASP实现远程保存图片
用ASP+DLL实现WEB方式修改服务器时间
ASP使用MySQL数据库全攻略
ASP+SQL Server构建网页防火墙
教程/ASP 十天学会ASP之第二天
教程/ASP 十天学会ASP之第四天
教程/ASP 十天学会ASP之第五天
教程/ASP 十天学会ASP之第六天
教程/ASP 十天学会ASP之第七天
教程/ASP 十天学会ASP之第八天
教程/ASP 十天学会ASP之第九天
教程/ASP 十天学会ASP之第十天
关于学习ASP和编程的28个观点

ASP.NET: XML留言版第二版


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 25 ::
收藏到网摘: n/a

  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.MoveToP