用一个留言簿说明C#操作XML的完全过程
2024-09-05 20:55:45
供稿:网友
第一步:在项目文件里创建db文件夹,在db文件夹里创建db.xml文档。db.xml结构只包含:<group></group>
第二步:新建添加留言和显示留言的两个画面:messageadd.aspx和messagelist.aspx。下面是两个页面的 前台和后台源码。
1.messageadd.aspx
<%@ page language="c#" codebehind="messageadd.aspx.cs" autoeventwireup="false" inherits="xmlguest.messageadd" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>messageadd</title>
<meta content="microsoft visual studio 7.0" name="generator">
<meta content="c#" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
<style>.txt1 {
font-size: 12px
}
</style>
</head>
<body>
<form id="messageadd" method="post" runat="server">
<font face="宋体">
<p> </p>
<p align="center">
<table class="txt1" id="table1" cellspacing="1" cellpadding="4" width="749" bgcolor="#efefef"
border="0">
<tr>
<td bgcolor="#e8dabd">您的大名</td>
<td style="width: 508px" bgcolor="#ffffff"><asp:textbox id="textboxuname" runat="server" leavesql="false" leaveformat="false" htmlencode="false"
width="519px" maxlength="10" cssclass="textareaclass"></asp:textbox></td>
<td bgcolor="#ffffff" colspan="1" rowspan="1"></td>
</tr>
<tr>
<td bgcolor="#e8dabd" colspan="1" rowspan="1">e-mail</td>
<td style="width: 505px" bgcolor="#ffffff">
<p><asp:textbox id="textboxmail" runat="server" leavesql="false" leaveformat="false" htmlencode="false"
width="519px" maxlength="30" cssclass="textareaclass"></asp:textbox></p>
</td>
<td bgcolor="#ffffff" colspan="1" rowspan="1"><br>
</td>
</tr>
<tr>
<td bgcolor="#e8dabd" colspan="1" rowspan="1">内容</td>
<td style="width: 505px" bgcolor="#ffffff">
<p><asp:textbox id="textboxtxt" runat="server" leavesql="false" leaveformat="true" htmlencode="false"
width="519px" textmode="multiline" height="160px"></asp:textbox></p>
</td>
<td bgcolor="#ffffff" colspan="1" rowspan="1"></td>
</tr>
</table>
</p>
<p align="center"><asp:button id="button1" runat="server" cssclass="textareaclass" text=" 提交 "></asp:button>
<input class="textareaclass" onclick="window.location.replace(messagelist.aspx)" type="button"
value=" 返回 "></p>
</font>
</form>
</body>
</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
{
/// <summary>
/// messageadd 的摘要说明。
/// </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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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
<%@ page language="c#" codebehind="messagelist.aspx.cs" autoeventwireup="false" inherits="xmlguest.messagelist" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>messagelist</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
<style>
.body { font-size: 12px }
.td1 { font-size: 12px }
</style>
</head>
<body class="body">
<form id="messagelist" method="post" runat="server">
<p align="right">
<asp:hyperlink id="growhyperlink1" runat="server" text="[留言]" navigateurl="messageadd.aspx"></asp:hyperlink>
</p>
<p align="center"><font face="宋体"></font><font face="宋体"></font><br>
<asp:datagrid id="datalist1" runat="server" autogeneratecolumns="false" showheader="false" pagesize="15"
borderstyle="none" borderwidth="0px" cellpadding="4" font-size="12px">
<columns>
<asp:templatecolumn>
<itemtemplate>
<table id="table7" bordercolor="#efefef" cellspacing="0" cellpadding="4" width="749" bgcolor="#efefef"
border="1">
<tr class="td1" bgcolor="#fafafa">
<td style="width: 200px" bgcolor="#ffffff"><strong>姓名:</strong>
<asp:label id=label5 runat="server" text=<%# databinder.eval(container, "dataitem.username").tostring() %>>
</asp:label>
</td>
<td style="width: 150px" bgcolor="#ffffff"><strong><font face="宋体">时间</font></strong><strong><font face="宋体">:</font></strong>
<asp:label id=label4 runat="server" text=<%# databinder.eval(container, "dataitem.sendtime").tostring() %>>
</asp:label></td>
<td bgcolor="#ffffff"><strong>e-mail</strong><strong>:
<asp:hyperlink id=hyperlink1 runat="server" navigateurl=<%# "mailto:"+databinder.eval(container, "dataitem.useremail").tostring() %> text=<%# databinder.eval(container, "dataitem.useremail").tostring() %>>
</asp:hyperlink></strong></td>
</tr>
<tr class="td1">
<td bgcolor="#ffffff" colspan="3">
<asp:label id=label1 runat="server" text=<%# databinder.eval(container, "dataitem.content").tostring() %>>
</asp:label></td>
</tr>
</table>
</itemtemplate>
</asp:templatecolumn>
</columns>
<pagerstyle visible="false"></pagerstyle>
</asp:datagrid>
</p>
</form>
</body>
</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
{
/// <summary>
/// messagelist 的摘要说明。
/// </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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。