我在asp.net c#中创建了非常简单的讨论论坛。它是从xml文件加载和插入数据的转发器。从xml文件加载注释通常可以正常工作,但在将新注释插入xml文件后却没有。单击插入按钮后,它会在xml文件中插入新注释,但转发器无法从同一个xml文件加载任何数据。我尝试在点击事件中重新绑定转发器,但它不起作用。
这是我将数据插入XML的代码(使用xmldocument类)。
XmlNode author = doc.SelectSingleNode(string.Format("//author[@id={0}]",Request.QueryString["id"]));
XmlNode comment = doc.CreateNode(XmlNodeType.Element, "comment", "");
XmlNode name = doc.CreateNode(XmlNodeType.Element, "name", "");
name.InnerText = nameTxb.Text.Trim();
XmlNode date = doc.CreateNode(XmlNodeType.Element, "date", "");
date.InnerText = string.Format("{0:D}", DateTime.Now);
XmlNode message = doc.CreateNode(XmlNodeType.Element, "message", "");
message.InnerText = messageTxb.Text.Trim();
comment.AppendChild(name);
comment.AppendChild(date);
comment.AppendChild(message);
author.AppendChild(comment);
doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
nameTxb.Text = "";
messageTxb.Text = "";
Repeater1.DataSourceID = "XmlDataSource1";
Repeater1.DataBind();
XML片段如下所示:
<comment>
<name>...</name>
<date>...</date>
<message>...</message>
</comment>
答案 0 :(得分:0)
保存xml后需要设置XmlDataSource1.DataFile
,可能还需要设置如下所示的XPath:
doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
nameTxb.Text = "";
messageTxb.Text = "";
XmlDataSource1.DataFile = Server.MapPath("~/App_Data/discussion.xml");
XmlDataSource1.XPath = "root/author[@id='" + Request.QueryString["id"] + "']/comment";//root is the root in discussion.xml
以下是我测试代码的方法:
我在discussion.xml
中有以下xml文件App_Data
:
<?xml version="1.0" encoding="utf-8"?>
<root>
<author id="1">
<comment>
<name>John Doe</name>
<date>Wednesday, December 4, 2013</date>
<message>Test Message</message>
</comment>
</author>
</root>
这是我的标记:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
<ItemTemplate>
<h3><%# XPath ("name") %></h3>
<b>Date:</b>
<%# XPath ("date") %>
</ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_Data/discussion.xml"
XPath="root/author[@id='1']/comment">
</asp:XmlDataSource>
<asp:TextBox ID="nameTxb" runat="server"></asp:TextBox>
<asp:TextBox ID="messageTxb" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
在代码隐藏中我有这种方法:
protected void Button1_Click(object sender, EventArgs e)
{
string auid = String.IsNullOrEmpty(Request.QueryString["id"]) ? "1" : Request.QueryString["id"];
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/App_Data/discussion.xml"));
XmlNode author = doc.SelectSingleNode(string.Format("//author[@id={0}]", auid));
XmlNode comment = doc.CreateNode(XmlNodeType.Element, "comment", "");
comment.InnerText = messageTxb.Text;
XmlNode name = doc.CreateNode(XmlNodeType.Element, "name", "");
name.InnerText = nameTxb.Text.Trim();
XmlNode date = doc.CreateNode(XmlNodeType.Element, "date", "");
date.InnerText = string.Format("{0:D}", DateTime.Now);
XmlNode message = doc.CreateNode(XmlNodeType.Element, "message", "");
message.InnerText = messageTxb.Text.Trim();
comment.AppendChild(name);
comment.AppendChild(date);
comment.AppendChild(message);
author.AppendChild(comment);
doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
nameTxb.Text = "";
messageTxb.Text = "";
XmlDataSource1.DataFile = Server.MapPath("~/App_Data/discussion.xml");
XmlDataSource1.XPath = "root/author[@id='" + auid + "']/comment";
Repeater1.DataBind();
}