XML文件,可以作为数据库来存取。 建立文件XMLData.xml <?xml version="1.0" standalone="yes"?> <MsgBoard> <Messages> <NoID>1</NoID> <UserName>Hona</UserName> <MsgTitle>写给peipei_t的留言</MsgTitle> <MsgContent> 这个网站是用XML与ASP.NET做的 </MsgContent> <WriteTime>2007-2-18 19:29:10</WriteTime> </Messages> </MsgBoard> 建立显示页面TestXML.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestXML.aspx.cs" Inherits="TestXML" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> 呢称:<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label><br /> 标题:<asp:Label ID="Label2" runat="server" Text='<%# Eval("MsgTitle") %>'></asp:Label><br /> 内容:<asp:Label ID="Label3" runat="server" Text='<%# Eval("MsgContent") %>'></asp:Label><br /> 时间:<asp:Label ID="Label4" runat="server" Text='<%# Eval("WriteTime") %>'></asp:Label><br /> <br /> </ItemTemplate> </asp:DataList> 呢称:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> 标题:<asp:TextBox ID="txtTile" runat="server"></asp:TextBox><br /> 内容:<asp:TextBox ID="txtContent" runat="server" Height="55px" TextMode="MultiLine" Width="226px"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Height="26px" οnclick="Button1_Click" Text="确定" /> </form> </body> </html> TestXML.aspx.cs using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public partial class TestXML : System.Web.UI.Page { private DataSet dsXML; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataBindXML(); } } //读取XML文件 private void DataBindXML() { dsXML = new DataSet(); dsXML.ReadXml(Server.MapPath("XMLData.xml")); DataList1.DataSource = dsXML; DataList1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { dsXML = new DataSet(); dsXML.ReadXml(Server.MapPath("XMLData.xml")); //产生一个新行 DataRow newRow=dsXML.Tables["Messages"].NewRow(); //向新行中增加数据 newRow["NoID"] = dsXML.Tables["Messages"].Rows.Count + 1; newRow["UserName"] = txtName.Text.Trim(); newRow["MsgTitle"] = txtTile.Text.Trim(); newRow["MsgContent"] = txtContent.Text.Trim(); newRow["WriteTime"] = DateTime.Now.ToShortDateString(); //把行添加到行集合中 dsXML.Tables["Messages"].Rows.Add(newRow); //写入到XML文件 dsXML.WriteXml(Server.MapPath("XMLData.xml")); DataBindXML(); } } 运行效果: