用一个留言簿说明C#操作XML的完全过程

第一步:在项目文件里创建db文件夹,在db文件夹里创建db.xml文档。 db.xml结构只包含:

1<group></group>

第二步:新建添加留言和显示留言的两个画面:MessageAdd.aspx和MessageList.aspx。下面是两个页面的 前台和后台源码。

//--------------------Begin--------------------//

1.MessageAdd.aspx

1@ Page language="c#" Codebehind="MessageAdd.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageAdd" 
 1<html>
 2<head>
 3<title>MessageAdd</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8<style>.Txt1 {   
 9FONT-SIZE: 12px   
10}   
11</style>
12</head>
13<body>
14<form id="MessageAdd" method="post" runat="server">
15<font face="宋体">
16<p> </p>
17<p align="center">
18<table bgcolor="#efefef" border="0" cellpadding="4" cellspacing="1" class="Txt1" id="Table1" width="749">
19<tr>
20<td bgcolor="#e8dabd">您的大名</td>
21<td bgcolor="#ffffff" style="WIDTH: 508px"><asp:textbox cssclass="TextAreaClass" htmlencode="False" id="TextBoxUName" leaveformat="False" leavesql="False" maxlength="10" runat="server" width="519px"></asp:textbox></td>
22<td bgcolor="#ffffff" colspan="1" rowspan="1"></td>
23</tr>
24<tr>
25<td bgcolor="#e8dabd" colspan="1" rowspan="1">E-Mail</td>
26<td bgcolor="#ffffff" style="WIDTH: 505px">
27<p><asp:textbox cssclass="TextAreaClass" htmlencode="False" id="TextBoxMail" leaveformat="False" leavesql="False" maxlength="30" runat="server" width="519px"></asp:textbox></p>
28</td>
29<td bgcolor="#ffffff" colspan="1" rowspan="1"><br/>
30</td>
31</tr>
32<tr>
33<td bgcolor="#e8dabd" colspan="1" rowspan="1">内容</td>
34<td bgcolor="#ffffff" style="WIDTH: 505px">
35<p><asp:textbox height="160px" htmlencode="False" id="TextBoxTxt" leaveformat="True" leavesql="False" runat="server" textmode="MultiLine" width="519px"></asp:textbox></p>
36</td>
37<td bgcolor="#ffffff" colspan="1" rowspan="1"></td>
38</tr>
39</table>
40</p>
41<p align="center"><asp:button cssclass="TextAreaClass" id="Button1" runat="server" text=" 提交 "></asp:button>       
42  <input class="TextAreaClass" onclick="window.location.replace('MessageList.aspx')" type="button" value=" 返回 "/></p>
43</font>
44</form>
45</body>
46</html>

MessageAdd.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace XMLGuest
{
///

1<summary>   
2/// MessageAdd 的摘要说明。   
3/// </summary>

public class MessageAdd : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBoxUName;
protected System.Web.UI.WebControls.TextBox TextBoxMail;
protected System.Web.UI.WebControls.TextBox TextBoxTxt;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{

XmlDocument doc = new XmlDocument();
doc.Load(MapPath("db\\db.xml"));
XmlNode node = doc.CreateElement("Item");

XmlAttribute xmlattr = doc.CreateAttribute("UserName");
xmlattr.Value = XmlConvert.DecodeName(TextBoxUName.Text);
node.Attributes.Append(xmlattr);

xmlattr = doc.CreateAttribute("UserEmail");
xmlattr.Value = XmlConvert.DecodeName(TextBoxMail.Text);
node.Attributes.Append(xmlattr);

xmlattr = doc.CreateAttribute("Content");
xmlattr.Value = XmlConvert.DecodeName(TextBoxTxt.Text);
node.Attributes.Append(xmlattr);

xmlattr = doc.CreateAttribute("SendTime");
xmlattr.Value = XmlConvert.DecodeName(DateTime.Now.ToString());
node.Attributes.Append(xmlattr);

doc.ChildNodes[0].AppendChild(node);
doc.Save(MapPath("db\\db.xml"));
Response.Redirect("MessageList.aspx");

}
}
}

2.MessageList.aspx

1@ Page language="c#" Codebehind="MessageList.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageList" 
 1<html>
 2<head>
 3<title>MessageList</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content="  http://schemas.microsoft.com/intellisense/ie5  " name="vs_targetSchema"/>
 8<style>   
 9.body { FONT-SIZE: 12px }   
10.td1 { FONT-SIZE: 12px }   
11</style>
12</head>
13<body class="body">
14<form id="MessageList" method="post" runat="server">
15<p align="right">
16<asp:hyperlink id="GrowHyperLink1" navigateurl="MessageAdd.aspx" runat="server" text="[留言]"></asp:hyperlink>              
17</p>
18<p align="center"><font face="宋体"></font><font face="宋体"></font><br/>
19<asp:datagrid autogeneratecolumns="False" borderstyle="None" borderwidth="0px" cellpadding="4" font-size="12px" id="DataList1" pagesize="15" runat="server" showheader="False">
20<columns>
21<asp:templatecolumn>
22<itemtemplate>
23<table bgcolor="#efefef" border="1" bordercolor="#efefef" cellpadding="4" cellspacing="0" id="Table7" width="749">
24<tr bgcolor="#fafafa" class="td1">
25<td bgcolor="#ffffff" style="WIDTH: 200px"><strong>姓名:</strong>
26<asp:label id="Label5" runat="server" text='```
27# DataBinder.Eval(Container, "DataItem.UserName").ToString() 
28```'>
29</asp:label>     
30</td>
31<td bgcolor="#ffffff" style="WIDTH: 150px"><strong><font face="宋体">时间</font></strong><strong><font face="宋体">:</font></strong>
32<asp:label id="Label4" runat="server" text='```
33# DataBinder.Eval(Container, "DataItem.SendTime").ToString() 
34```'>
35</asp:label></td>
36<td bgcolor="#ffffff"><strong>E-Mail</strong><strong>:   
37<asp:hyperlink id="HyperLink1" navigateurl='```
38# "  mailto:"+DataBinder.Eval(Container  , "DataItem.UserEmail").ToString() 
39```' runat="server" text='```
40# DataBinder.Eval(Container, "DataItem.UserEmail").ToString() 
41```'>
42</asp:hyperlink></strong></td>
43</tr>
44<tr class="td1">
45<td bgcolor="#ffffff" colspan="3">
46<asp:label id="Label1" runat="server" text='```
47# DataBinder.Eval(Container, "DataItem.Content").ToString() 
48```'>
49</asp:label></td>
50</tr>
51</table>
52</itemtemplate>
53</asp:templatecolumn>
54</columns>
55<pagerstyle visible="False"></pagerstyle>
56</asp:datagrid>
57</p>
58</form>
59</body>
60</html>

MessageList.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;

namespace XMLGuest
{
///

1<summary>   
2/// MessageList 的摘要说明。   
3/// </summary>

public class MessageList : System.Web.UI.Page
{
protected HyperLink GrowHyperLink1;
protected System.Web.UI.WebControls.DataGrid DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
FileStream stream = new FileStream(Server.MapPath("db\\db.xml"),FileMode.Open);
try
{
XmlDataDocument doc = new XmlDataDocument();
doc.DataSet.ReadXml(new StreamReader(stream));
if(doc.DataSet.Tables.Count != 0 && doc.DataSet.Tables[0].Rows.Count!=0)
{
DataList1.DataSource = doc.DataSet.Tables[0].DefaultView ;
DataList1.DataBind();
}
stream.Close();
}
catch(Exception ding)
{
Response.Write(ding.Message);
}
finally
{
stream.Close();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

//---------------------End----------------------//

Published At
Categories with Web编程
Tagged with
comments powered by Disqus