c#操作xml实例(简单留言本)
default.aspx前台界面代码:

Code
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head id="Head1" runat="server">
7
<title>留言本首页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<table style="width: 550px; height: 150px" align="center" border="1" cellpadding="0" cellspacing="0">
13
<tr>
14
<td style="height: 65px; font-weight: bold; font-size: 24pt; text-align: center;">
15
留言本</td>
16
</tr>
17
<tr>
18
<td style="font-size: 9pt; vertical-align: middle; text-align: right">
19
<asp:HyperLink ID="HyperLink1" runat="server" Font-Size="9pt" Font-Underline="False"
20
NavigateUrl="~/Default2.aspx">添加新留言</asp:HyperLink> </td>
21
</tr>
22
<tr>
23
<td style="font-size: 9pt; vertical-align: top; text-align: center">
24
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
25
26
</tr>
27
<tr>
28
<td style="height: 50px; font-size: 9pt; text-align: center;">
29
<a href="" target="mainframe" style="font-size: 9pt;text-decoration:none; color:Blue;"></a>tao
30
</td>
31
</tr>
32
</table>
33
34
</div>
35
</form>
36
</body>
37
</html>
38
default.aspx.cs

Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Xml;
11
using System.Xml.Xsl;
12
13
public partial class _Default : System.Web.UI.Page
14

{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
XmlDocument doc = new XmlDocument();
18
doc.Load(Server.MapPath("list.xml"));
19
XslTransform trans = new XslTransform();
20
trans.Load(Server.MapPath("list.xsl"));
21
Xml1.Document = doc;
22
Xml1.Transform = trans;
23
}
24
}
default2.aspx

Code
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<table style="width: 550px;" align="center" border="1" cellpadding="0" cellspacing="0">
13
<tr>
14
<td style="height: 50px; font-weight: bold; font-size: 24pt; vertical-align: middle; text-align: center;" colspan="2">
15
留言本</td>
16
</tr>
17
<tr>
18
<td colspan="2" style="font-size: 9pt; height: 12px; text-align: right">
19
<asp:HyperLink ID="HyperLink1" runat="server" Font-Size="9pt" Font-Underline="False"
20
NavigateUrl="~/Default.aspx">返回首页</asp:HyperLink> </td>
21
</tr>
22
<tr>
23
<td style="width: 74px; height: 25px; font-size: 9pt; text-align: center;">
24
标题:</td>
25
<td style="height: 25px">
26
<asp:TextBox ID="TextBox1" runat="server" Width="459px"></asp:TextBox></td>
27
</tr>
28
<tr>
29
<td style="font-size: 9pt; vertical-align: top; width: 74px; text-align: center">
30
留言内容:</td>
31
<td>
32
<asp:TextBox ID="TextBox2" runat="server" Height="162px" TextMode="MultiLine" Width="459px"></asp:TextBox></td>
33
</tr>
34
<tr>
35
<td colspan="2" style="height: 26px; text-align: center;">
36
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="提交" />
37
38
<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="重置" OnClick="Button2_Click" /></td>
39
</tr>
40
</table>
41
</div>
42
</form>
43
</body>
44
</html>
45
default.aspx.cs

Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Xml;
12
13
public partial class Default2 : System.Web.UI.Page
14

{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
18
}
19
protected void Button1_Click(object sender, EventArgs e)
20
{
21
if (TextBox1.Text == "")
22
{
23
Response.Write("<script>alert('留言标题不能为空!')</script>");
24
}
25
else if (TextBox2.Text == "")
26
{
27
Response.Write("<script>alert('留言内容不能为空!')</script>");
28
}
29
else
30
{
31
XmlDocument doc = new XmlDocument();
32
doc.Load(Server.MapPath("list.xml"));
33
XmlNode newNode1;
34
XmlNode newNode2;
35
newNode1 = doc.CreateElement("LIST");
36
newNode2 = doc.CreateElement("TITLE");
37
newNode2.InnerText = TextBox1.Text;
38
newNode1.AppendChild(newNode2);
39
newNode2 = doc.CreateElement("POSTTIME");
40
newNode2.InnerText = DateTime.Now.ToString();
41
newNode1.AppendChild(newNode2);
42
newNode2 = doc.CreateElement("TEXT");
43
newNode2.InnerText = TextBox2.Text;
44
newNode1.AppendChild(newNode2);
45
doc.DocumentElement.AppendChild(newNode1);
46
doc.Save(Server.MapPath("list.xml"));
47
Response.Write("<script>alert('添加留言成功!')</script>");
48
TextBox1.Text = TextBox2.Text = string.Empty;
49
}
50
}
51
protected void Button2_Click(object sender, EventArgs e)
52
{
53
TextBox1.Text = "";
54
TextBox2.Text = "";
55
}
56
}
57
list.xml

Code
<?xml version="1.0" encoding="utf-8"?>
<NEWLIST>
<LIST>
<TITLE>一个心情!!</TITLE>
<POSTTIME>2006-9-4 15:53:42</POSTTIME>
<TEXT>一个不再永远的话题,生活很累,但需要奋斗!</TEXT>
</LIST>
<LIST>
<TITLE>新的感悟!!</TITLE>
<POSTTIME>2006-9-5 16:29:50</POSTTIME>
<TEXT>每一天的工作,每一天的心情,每一天的事件,每一天的想法,每一天生活。</TEXT>
</LIST>
<LIST>
<TITLE>随心所意</TITLE>
<POSTTIME>2006-9-5 16:33:25</POSTTIME>
<TEXT>生活因心情的好坏而变得美丽与无奈,所以我们必须认真地做好生活中的每一件事情,因为只有这样才能让人有一种永远不能气馁的想法!!</TEXT>
</LIST>
<LIST>
<TITLE>tao</TITLE>
<POSTTIME>2009-10-9 23:49:43</POSTTIME>
<TEXT>测试</TEXT>
</LIST>
</NEWLIST>list.xsl:

Code
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table border="1" cellpadding="0" cellspacing="0" style="width: 550px;" >
<xsl:for-each select="NEWLIST/LIST">
<tr>
<td colspan="2" style="font-size: 9pt; width: 40px; height: 30px; text-align: center">
标题:
</td>
<td style="font-size: 9pt; text-align: left; height: 30px;">
<xsl:value-of select="TITLE"/>
</td>
<td style="font-size: 9pt; text-align: center; width: 60px; height: 30px;">
发表时间:
</td>
<td style="font-size: 9pt; text-align: left; height: 30px;">
<xsl:value-of select="POSTTIME"/>
</td>
</tr>
<tr height="30">
<td colspan="5" style="font-size: 9pt; text-align: left">
<table>
<tr>
<td style="width: 20px">
</td>
<td>
<xsl:value-of select="TEXT"/>
</td>
</tr>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head id="Head1" runat="server">7
<title>留言本首页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
<table style="width: 550px; height: 150px" align="center" border="1" cellpadding="0" cellspacing="0">13
<tr>14
<td style="height: 65px; font-weight: bold; font-size: 24pt; text-align: center;">15
留言本</td>16
</tr>17
<tr>18
<td style="font-size: 9pt; vertical-align: middle; text-align: right">19
<asp:HyperLink ID="HyperLink1" runat="server" Font-Size="9pt" Font-Underline="False"20
NavigateUrl="~/Default2.aspx">添加新留言</asp:HyperLink> </td>21
</tr>22
<tr>23
<td style="font-size: 9pt; vertical-align: top; text-align: center">24
<asp:Xml ID="Xml1" runat="server"></asp:Xml>25
26
</tr>27
<tr>28
<td style="height: 50px; font-size: 9pt; text-align: center;">29
<a href="" target="mainframe" style="font-size: 9pt;text-decoration:none; color:Blue;"></a>tao 30
</td>31
</tr>32
</table>33
34
</div>35
</form>36
</body>37
</html>38

default.aspx.cs
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10
using System.Xml;11
using System.Xml.Xsl;12

13
public partial class _Default : System.Web.UI.Page 14


{15
protected void Page_Load(object sender, EventArgs e)16

{17
XmlDocument doc = new XmlDocument();18
doc.Load(Server.MapPath("list.xml"));19
XslTransform trans = new XslTransform();20
trans.Load(Server.MapPath("list.xsl"));21
Xml1.Document = doc;22
Xml1.Transform = trans;23
}24
}default2.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
<table style="width: 550px;" align="center" border="1" cellpadding="0" cellspacing="0">13
<tr>14
<td style="height: 50px; font-weight: bold; font-size: 24pt; vertical-align: middle; text-align: center;" colspan="2">15
留言本</td>16
</tr>17
<tr>18
<td colspan="2" style="font-size: 9pt; height: 12px; text-align: right">19
<asp:HyperLink ID="HyperLink1" runat="server" Font-Size="9pt" Font-Underline="False"20
NavigateUrl="~/Default.aspx">返回首页</asp:HyperLink> </td>21
</tr>22
<tr>23
<td style="width: 74px; height: 25px; font-size: 9pt; text-align: center;">24
标题:</td>25
<td style="height: 25px">26
<asp:TextBox ID="TextBox1" runat="server" Width="459px"></asp:TextBox></td>27
</tr>28
<tr>29
<td style="font-size: 9pt; vertical-align: top; width: 74px; text-align: center">30
留言内容:</td>31
<td>32
<asp:TextBox ID="TextBox2" runat="server" Height="162px" TextMode="MultiLine" Width="459px"></asp:TextBox></td>33
</tr>34
<tr>35
<td colspan="2" style="height: 26px; text-align: center;">36
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="提交" />37
38
<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="重置" OnClick="Button2_Click" /></td>39
</tr>40
</table>41
</div>42
</form>43
</body>44
</html>45

1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Xml;12

13
public partial class Default2 : System.Web.UI.Page14


{15
protected void Page_Load(object sender, EventArgs e)16

{17

18
}19
protected void Button1_Click(object sender, EventArgs e)20

{21
if (TextBox1.Text == "")22

{23
Response.Write("<script>alert('留言标题不能为空!')</script>");24
}25
else if (TextBox2.Text == "")26

{27
Response.Write("<script>alert('留言内容不能为空!')</script>");28
}29
else30

{31
XmlDocument doc = new XmlDocument();32
doc.Load(Server.MapPath("list.xml"));33
XmlNode newNode1;34
XmlNode newNode2;35
newNode1 = doc.CreateElement("LIST");36
newNode2 = doc.CreateElement("TITLE");37
newNode2.InnerText = TextBox1.Text;38
newNode1.AppendChild(newNode2);39
newNode2 = doc.CreateElement("POSTTIME");40
newNode2.InnerText = DateTime.Now.ToString();41
newNode1.AppendChild(newNode2);42
newNode2 = doc.CreateElement("TEXT");43
newNode2.InnerText = TextBox2.Text;44
newNode1.AppendChild(newNode2);45
doc.DocumentElement.AppendChild(newNode1);46
doc.Save(Server.MapPath("list.xml"));47
Response.Write("<script>alert('添加留言成功!')</script>");48
TextBox1.Text = TextBox2.Text = string.Empty;49
}50
}51
protected void Button2_Click(object sender, EventArgs e)52

{53
TextBox1.Text = "";54
TextBox2.Text = "";55
}56
}57

<?xml version="1.0" encoding="utf-8"?>
<NEWLIST>
<LIST>
<TITLE>一个心情!!</TITLE>
<POSTTIME>2006-9-4 15:53:42</POSTTIME>
<TEXT>一个不再永远的话题,生活很累,但需要奋斗!</TEXT>
</LIST>
<LIST>
<TITLE>新的感悟!!</TITLE>
<POSTTIME>2006-9-5 16:29:50</POSTTIME>
<TEXT>每一天的工作,每一天的心情,每一天的事件,每一天的想法,每一天生活。</TEXT>
</LIST>
<LIST>
<TITLE>随心所意</TITLE>
<POSTTIME>2006-9-5 16:33:25</POSTTIME>
<TEXT>生活因心情的好坏而变得美丽与无奈,所以我们必须认真地做好生活中的每一件事情,因为只有这样才能让人有一种永远不能气馁的想法!!</TEXT>
</LIST>
<LIST>
<TITLE>tao</TITLE>
<POSTTIME>2009-10-9 23:49:43</POSTTIME>
<TEXT>测试</TEXT>
</LIST>
</NEWLIST>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table border="1" cellpadding="0" cellspacing="0" style="width: 550px;" >
<xsl:for-each select="NEWLIST/LIST">
<tr>
<td colspan="2" style="font-size: 9pt; width: 40px; height: 30px; text-align: center">
标题:
</td>
<td style="font-size: 9pt; text-align: left; height: 30px;">
<xsl:value-of select="TITLE"/>
</td>
<td style="font-size: 9pt; text-align: center; width: 60px; height: 30px;">
发表时间:
</td>
<td style="font-size: 9pt; text-align: left; height: 30px;">
<xsl:value-of select="POSTTIME"/>
</td>
</tr>
<tr height="30">
<td colspan="5" style="font-size: 9pt; text-align: left">
<table>
<tr>
<td style="width: 20px">
</td>
<td>
<xsl:value-of select="TEXT"/>
</td>
</tr>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet> 

浙公网安备 33010602011771号