这个例题非常十分特别太简单了,后面会有一个比较完整的,包括后台管理,增删改的例题,主要我还有点问题没有弄明白,别说简单就不要,呵呵,要知道没有简单的,以后只是一点问题,难道也要看复杂的那么麻烦吗?最重要的是简单的这个源码都在,不需要下载!

XML代码
1
<?xml version="1.0" standalone="yes"?>
2
<NewDataSet xmlns="http://tempuri.org/db1.xsd">
3
<user>
4
<name>天轰穿</name>
5
<email>thc@Fsd.com</email>
6
<diqu>四川</diqu>
7
<concent>哈哈,我还以为是什么呢,原来是这个玩意!</concent>
8
</user>
9
<user>
10
<name>田洪川</name>
11
<email>gfd@Fds.com</email>
12
<diqu>四川</diqu>
13
<concent>dsgdsgfdbfdbfcvfdvfdzvfd</concent>
14
</user>
15
<user>
16
<name>黄小梅</name>
17
<email>dsf@fddd.com</email>
18
<diqu>四川</diqu>
19
<concent>反对撒法发是犯得上但是 打出手</concent>
20
</user>
21
<user>
22
<name>test</name>
23
<email>test@qq.com</email>
24
<diqu>广东</diqu>
25
<concent>dsacdscdsacdsacsa</concent>
26
</user>
27
</NewDataSet>

后台C#代码
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
12
namespace XML
13

{
14
/**//// <summary>
15
/// book 的摘要说明。
16
/// </summary>
17
public class book : System.Web.UI.Page
18
{
19
protected System.Web.UI.WebControls.TextBox tbname;
20
protected System.Web.UI.WebControls.TextBox tbemail;
21
protected System.Web.UI.WebControls.TextBox tbconcent;
22
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
23
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
24
protected System.Web.UI.WebControls.DropDownList DropDownList1;
25
protected System.Web.UI.WebControls.DataGrid dg;
26
protected System.Web.UI.WebControls.Button tbn;
27
28
private void Page_Load(object sender, System.EventArgs e)
29
{
30
fill();
31
}
32
33
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
34
override protected void OnInit(EventArgs e)
35
{
36
//
37
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
38
//
39
InitializeComponent();
40
base.OnInit(e);
41
}
42
43
/**//// <summary>
44
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
45
/// 此方法的内容。
46
/// </summary>
47
private void InitializeComponent()
48
{
49
this.tbn.Click += new System.EventHandler(this.tbn_Click);
50
this.Load += new System.EventHandler(this.Page_Load);
51
52
}
53
#endregion
54
55
private void fill()
56
{
57
// 读取XML文件的数据到DataGrid
58
DataSet ds=new DataSet();//声明一个读取器
59
ds.ReadXml(Server.MapPath("\\xml\\xml\\db1.xml"));//找到路径
60
this.dg.DataSource=ds.Tables[0].DefaultView;//指定dg网格的数据源
61
this.dg.DataBind();//绑定
62
63
// 这段有问题,如果有那位朋友知道的话,万望赐教,谢谢,我QQ110535808,感激不尽
64
// for(int i=0;i<ds.Tables[0].Rows.Count;i++)
65
// {
66
// string email=ds.Tables[0].Rows[i]["email"].ToString();
67
// System.Web.UI.WebControls.Image img=(System.Web.UI.WebControls.Image)(dg.FindControl("img"));
68
// Response.Write(img);
69
// img.ImageUrl=(Server.MapPath("\\xml\\img\\e.gif"));
70
// img.ToolTip=email;
71
// }
72
}
73
private void tbn_Click(object sender, System.EventArgs e)
74
{//提交留言
75
DataSet ds=new DataSet("newds");//建立一个新的内存库
76
ds.ReadXml(Server.MapPath("\\xml\\xml\\db1.xml"));//读取XML文件
77
DataRow dr=ds.Tables[0].NewRow();//给各行赋值
78
dr["name"]=this.tbname.Text.ToString();
79
dr["email"]=this.tbemail.Text.ToString();
80
dr["diqu"]=this.DropDownList1.SelectedValue.ToString();
81
dr["concent"]=this.tbconcent.Text.ToString();
82
ds.Tables[0].Rows.Add(dr);//给内存表中增加一行数据
83
//写进文件
84
ds.WriteXml(Server.MapPath("\\xml\\xml\\db1.xml"));//写进文件,关键
85
fill(); //重新绑定
86
}
87
}
88
}
89

前台HTM代码
1
<%
@ Page language="c#" Codebehind="book.aspx.cs" AutoEventWireup="false" Inherits="XML.book" %>
2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3
<HTML>
4
<HEAD>
5
<title>book</title>
6
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
7
<meta name="CODE_LANGUAGE" Content="C#">
8
<meta name="vs_defaultClientScript" content="JavaScript">
9
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10
</HEAD>
11
<body MS_POSITIONING="GridLayout">
12
<form id="Form1" method="post" runat="server">
13
<FONT face="宋体">
14
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 608px; POSITION: absolute; TOP: 32px; HEIGHT: 352px"
15
cellSpacing="0" borderColorDark="#99ff00" cellPadding="0" width="608" border="1">
16
<TR>
17
<TD style="HEIGHT: 38px" align="center" bgColor="#99ff00"><STRONG>基于XML的留言本</STRONG></TD>
18
</TR>
19
<TR>
20
<TD>
21
<TABLE id="Table2" style="WIDTH: 784px; HEIGHT: 260px" cellSpacing="0" cellPadding="0"
22
width="784" border="1">
23
<TR>
24
<TD style="HEIGHT: 38px">姓名:</TD>
25
<TD style="HEIGHT: 38px">
26
<asp:TextBox id="tbname" runat="server"></asp:TextBox>
27
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="tbname"></asp:RequiredFieldValidator></TD>
28
</TR>
29
<TR>
30
<TD style="HEIGHT: 41px">邮件:</TD>
31
<TD style="HEIGHT: 41px">
32
<asp:TextBox id="tbemail" runat="server"></asp:TextBox>
33
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="错误的格式" ControlToValidate="tbemail"
34
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></TD>
35
</TR>
36
<TR>
37
<TD style="HEIGHT: 43px">地区:</TD>
38
<TD style="HEIGHT: 43px">
39
<asp:DropDownList id="DropDownList1" runat="server">
40
<asp:ListItem Value="四川">四川</asp:ListItem>
41
<asp:ListItem Value="广东">广东</asp:ListItem>
42
<asp:ListItem Value="天津">天津</asp:ListItem>
43
<asp:ListItem Value="黑龙江">黑龙江</asp:ListItem>
44
<asp:ListItem Value="蒙古">蒙古</asp:ListItem>
45
</asp:DropDownList></TD>
46
</TR>
47
<TR>
48
<TD>留言内容:</TD>
49
<TD>
50
<asp:TextBox id="tbconcent" runat="server" TextMode="MultiLine" Height="83px" Width="341px"></asp:TextBox></TD>
51
</TR>
52
<TR>
53
<TD></TD>
54
<TD>
55
<asp:Button id="tbn" runat="server" Text="提交"></asp:Button></TD>
56
</TR>
57
</TABLE>
58
</TD>
59
</TR>
60
<TR>
61
<TD>
62
<asp:DataGrid id="dg" runat="server" Width="784px" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
63
BackColor="White" CellPadding="4" AutoGenerateColumns="False">
64
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
65
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
66
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
67
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
68
<Columns>
69
<asp:BoundColumn DataField="name" HeaderText="用户姓名"></asp:BoundColumn>
70
<asp:BoundColumn DataField="email" HeaderText="邮件"></asp:BoundColumn>
71
<asp:BoundColumn DataField="diqu" HeaderText="城市"></asp:BoundColumn>
72
<asp:BoundColumn DataField="concent" HeaderText="内容"></asp:BoundColumn>
73
<asp:TemplateColumn HeaderText="修改后邮件">
74
<ItemTemplate>
75
<asp:Image id="img" runat="server"></asp:Image>
76
</ItemTemplate>
77
</asp:TemplateColumn>
78
</Columns>
79
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
80
</asp:DataGrid></TD>
81
</TR>
82
</TABLE>
83
</FONT>
84
</form>
85
</body>
86
</HTML>
87
1
<?xml version="1.0" standalone="yes"?>2
<NewDataSet xmlns="http://tempuri.org/db1.xsd">3
<user>4
<name>天轰穿</name>5
<email>thc@Fsd.com</email>6
<diqu>四川</diqu>7
<concent>哈哈,我还以为是什么呢,原来是这个玩意!</concent>8
</user>9
<user>10
<name>田洪川</name>11
<email>gfd@Fds.com</email>12
<diqu>四川</diqu>13
<concent>dsgdsgfdbfdbfcvfdvfdzvfd</concent>14
</user>15
<user>16
<name>黄小梅</name>17
<email>dsf@fddd.com</email>18
<diqu>四川</diqu>19
<concent>反对撒法发是犯得上但是 打出手</concent>20
</user>21
<user>22
<name>test</name>23
<email>test@qq.com</email>24
<diqu>广东</diqu>25
<concent>dsacdscdsacdsacsa</concent>26
</user>27
</NewDataSet>1
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11

12
namespace XML13


{14

/**//// <summary>15
/// book 的摘要说明。16
/// </summary>17
public class book : System.Web.UI.Page18

{19
protected System.Web.UI.WebControls.TextBox tbname;20
protected System.Web.UI.WebControls.TextBox tbemail;21
protected System.Web.UI.WebControls.TextBox tbconcent;22
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;23
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;24
protected System.Web.UI.WebControls.DropDownList DropDownList1;25
protected System.Web.UI.WebControls.DataGrid dg;26
protected System.Web.UI.WebControls.Button tbn;27
28
private void Page_Load(object sender, System.EventArgs e)29

{30
fill();31
}32

33

Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码34
override protected void OnInit(EventArgs e)35

{36
//37
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。38
//39
InitializeComponent();40
base.OnInit(e);41
}42
43

/**//// <summary>44
/// 设计器支持所需的方法 - 不要使用代码编辑器修改45
/// 此方法的内容。46
/// </summary>47
private void InitializeComponent()48

{ 49
this.tbn.Click += new System.EventHandler(this.tbn_Click);50
this.Load += new System.EventHandler(this.Page_Load);51

52
}53
#endregion54

55
private void fill()56

{57
// 读取XML文件的数据到DataGrid58
DataSet ds=new DataSet();//声明一个读取器59
ds.ReadXml(Server.MapPath("\\xml\\xml\\db1.xml"));//找到路径60
this.dg.DataSource=ds.Tables[0].DefaultView;//指定dg网格的数据源61
this.dg.DataBind();//绑定62

63
// 这段有问题,如果有那位朋友知道的话,万望赐教,谢谢,我QQ110535808,感激不尽64
// for(int i=0;i<ds.Tables[0].Rows.Count;i++)65
// {66
// string email=ds.Tables[0].Rows[i]["email"].ToString();67
// System.Web.UI.WebControls.Image img=(System.Web.UI.WebControls.Image)(dg.FindControl("img"));68
// Response.Write(img);69
// img.ImageUrl=(Server.MapPath("\\xml\\img\\e.gif"));70
// img.ToolTip=email;71
// }72
}73
private void tbn_Click(object sender, System.EventArgs e)74

{//提交留言75
DataSet ds=new DataSet("newds");//建立一个新的内存库76
ds.ReadXml(Server.MapPath("\\xml\\xml\\db1.xml"));//读取XML文件77
DataRow dr=ds.Tables[0].NewRow();//给各行赋值78
dr["name"]=this.tbname.Text.ToString();79
dr["email"]=this.tbemail.Text.ToString();80
dr["diqu"]=this.DropDownList1.SelectedValue.ToString();81
dr["concent"]=this.tbconcent.Text.ToString();82
ds.Tables[0].Rows.Add(dr);//给内存表中增加一行数据83
//写进文件84
ds.WriteXml(Server.MapPath("\\xml\\xml\\db1.xml"));//写进文件,关键85
fill(); //重新绑定86
}87
}88
}89

1

<%
@ Page language="c#" Codebehind="book.aspx.cs" AutoEventWireup="false" Inherits="XML.book" %>2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >3
<HTML>4
<HEAD>5
<title>book</title>6
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">7
<meta name="CODE_LANGUAGE" Content="C#">8
<meta name="vs_defaultClientScript" content="JavaScript">9
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">10
</HEAD>11
<body MS_POSITIONING="GridLayout">12
<form id="Form1" method="post" runat="server">13
<FONT face="宋体">14
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 608px; POSITION: absolute; TOP: 32px; HEIGHT: 352px"15
cellSpacing="0" borderColorDark="#99ff00" cellPadding="0" width="608" border="1">16
<TR>17
<TD style="HEIGHT: 38px" align="center" bgColor="#99ff00"><STRONG>基于XML的留言本</STRONG></TD>18
</TR>19
<TR>20
<TD>21
<TABLE id="Table2" style="WIDTH: 784px; HEIGHT: 260px" cellSpacing="0" cellPadding="0"22
width="784" border="1">23
<TR>24
<TD style="HEIGHT: 38px">姓名:</TD>25
<TD style="HEIGHT: 38px">26
<asp:TextBox id="tbname" runat="server"></asp:TextBox>27
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="tbname"></asp:RequiredFieldValidator></TD>28
</TR>29
<TR>30
<TD style="HEIGHT: 41px">邮件:</TD>31
<TD style="HEIGHT: 41px">32
<asp:TextBox id="tbemail" runat="server"></asp:TextBox>33
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="错误的格式" ControlToValidate="tbemail"34
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></TD>35
</TR>36
<TR>37
<TD style="HEIGHT: 43px">地区:</TD>38
<TD style="HEIGHT: 43px">39
<asp:DropDownList id="DropDownList1" runat="server">40
<asp:ListItem Value="四川">四川</asp:ListItem>41
<asp:ListItem Value="广东">广东</asp:ListItem>42
<asp:ListItem Value="天津">天津</asp:ListItem>43
<asp:ListItem Value="黑龙江">黑龙江</asp:ListItem>44
<asp:ListItem Value="蒙古">蒙古</asp:ListItem>45
</asp:DropDownList></TD>46
</TR>47
<TR>48
<TD>留言内容:</TD>49
<TD>50
<asp:TextBox id="tbconcent" runat="server" TextMode="MultiLine" Height="83px" Width="341px"></asp:TextBox></TD>51
</TR>52
<TR>53
<TD></TD>54
<TD>55
<asp:Button id="tbn" runat="server" Text="提交"></asp:Button></TD>56
</TR>57
</TABLE>58
</TD>59
</TR>60
<TR>61
<TD>62
<asp:DataGrid id="dg" runat="server" Width="784px" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"63
BackColor="White" CellPadding="4" AutoGenerateColumns="False">64
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>65
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>66
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>67
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>68
<Columns>69
<asp:BoundColumn DataField="name" HeaderText="用户姓名"></asp:BoundColumn>70
<asp:BoundColumn DataField="email" HeaderText="邮件"></asp:BoundColumn>71
<asp:BoundColumn DataField="diqu" HeaderText="城市"></asp:BoundColumn>72
<asp:BoundColumn DataField="concent" HeaderText="内容"></asp:BoundColumn>73
<asp:TemplateColumn HeaderText="修改后邮件">74
<ItemTemplate>75
<asp:Image id="img" runat="server"></asp:Image>76
</ItemTemplate>77
</asp:TemplateColumn>78
</Columns>79
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>80
</asp:DataGrid></TD>81
</TR>82
</TABLE>83
</FONT>84
</form>85
</body>86
</HTML>87

浙公网安备 33010602011771号