Web网站中,后置文件使用两种方式保存数据在何区别?请求帮助
注册页面代码:<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SingUp.aspx.cs" Inherits="SingUp" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 459px; text-align: center ; border:1px #ff000fff solid;">
<tr>
<td colspan="3" style="height: 23px;background-color:#3300ff; color: #ffffff; font-family:Microsoft Sans Serif;">
用户登录</td>
</tr>
<tr>
<td style="width: 101px" align="right">
用户名:</td>
<td style="width: 162px">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
<td style="width: 146px">
<asp:RequiredFieldValidator ID="rfvUserName" runat="server" ControlToValidate="txtUserName"
ErrorMessage="用户名不能为空!"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 101px" align="right">
密 码:</td>
<td style="width: 162px">
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
<td style="width: 146px">
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
ErrorMessage="密码不能为空!"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 101px; height: 18px" align="right">
请输入密码:</td>
<td style="width: 162px; height: 18px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td style="width: 146px; height: 18px">
<asp:CompareValidator ID="cvConfimPassword" runat="server" ControlToCompare="txtPassword"
ControlToValidate="TextBox1" ErrorMessage="密码不一样!"></asp:CompareValidator></td>
</tr>
<tr>
<td style="width: 101px">
</td>
<td style="width: 162px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
<asp:Button ID="btnCancle" runat="server" Text="取消" /></td>
<td style="width: 146px">
</td>
</tr>
</table>
</asp:Content>
这是后置文件代码: 第一种方式可以何存数据
protected void btnOK_Click(object sender, EventArgs e)
{
string path = Server.MapPath("Message.xml");
if (File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlElement newElement = xmlDoc.CreateElement("liuyan");
XmlElement elname = xmlDoc.CreateElement("name");
XmlElement elpass = xmlDoc.CreateElement("pass");
XmlElement elconfim = xmlDoc.CreateElement("confimps");
elname.InnerText = this.txtUserName.Text.Trim();
elpass.InnerText = this.txtPassword.Text.Trim();
elconfim.InnerText = this.TextBox1.Text.Trim();
newElement.AppendChild(elname);
newElement.AppendChild(elpass);
newElement.AppendChild(elconfim);
xmlDoc.DocumentElement.AppendChild(newElement);
xmlDoc.Save(Server.MapPath("Message.xml"));
Response.Write("<script>alert('你好')</script>");
}
}
public string ReturnCount()
{
string i = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("guestbook.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.LastChild;
if (xmlNode != null)
{
i = Convert.ToString(Convert.ToUInt32(xmlNode["id"].InnerText) + 1);
}
return i;
}
这种方式为什么不能保存数据
protected void btnOK_Click(object sender, EventArgs e)
{
string path = Server.MapPath("Message.xml");
if (!File.Exists(path))
{
DataTable dt = new DataTable("MessageTable");
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("Email", typeof(String));
dt.Columns.Add("QqNumber", typeof(String));
dt.Columns.Add("Information", typeof(String));
dt.Columns[0].AutoIncrement = true;
dt.Columns[0].AutoIncrementSeed = -1;
dt.Columns[0].AutoIncrementStep = -1;
DataRow dr = dt.NewRow();
dr[1] = this.txtUserName.Text.Trim();
dr[2] = this.txtPassword.Text.Trim();
dr[3] = this.TextBox1.Text.Trim();
dt.Rows.Add(dr);
DataSet ds = new DataSet("Root");
ds.Tables.Add(dt);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ds.GetXml());
ds.Clear();
ds.Dispose();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
//xmlDec.Encoding = "UTF-8";
//xmlDec.Standalone = "yes";
XmlElement xmlEleRoot = xmlDoc.DocumentElement;
xmlDoc.InsertBefore(xmlDec, xmlEleRoot);
xmlDoc.Save("MessageData.xml");
}
}