【问题标题】:Cannot replace child XmlDocument [duplicate]无法替换子 XmlDocument [重复]
【发布时间】:2014-02-01 18:55:08
【问题描述】:

我在 ASP.NET 中使用 XML 作为讨论论坛。我正在尝试添加更新 cmets 的功能。我的 XML 结构是这样的:

<forum>
 <author id="1">
  <comment id="0" idUser="19">
   <name>....</name>
   <date>....</date>
   <message>...</message>
  </comment>
  <comment id="1" idUser="4">
   ....
  </comment>
 </author>
 <author id="2">
  ....
 </author>
</forum>

我的代码是:

protected void btnEditComment_Click(object sender, EventArgs e)
{
    if (messageTxb.Text!="" && nameTxb.Text!="")
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/forum.xml"));
        XmlNode newComment = (XmlNode)Session["Comment"];
        XmlNode oldComment = doc.SelectSingleNode(string.Format("//author[@id={0}]/comment[@id={1}]",Request.QueryString["id"],newComment.Attributes["id"].Value));
        newComment.ChildNodes[0].InnerText = nameTxb.Text.Trim();
        newComment.ChildNodes[1].InnerText = string.Format("{0:D}", DateTime.Now);
        newComment.ChildNodes[2].InnerText = messageTxb.Text.Trim();
        oldComment.ParentNode.ReplaceChild(newComment, oldComment);
        doc.Save(Server.MapPath("~/App_Data/forum.xml"));
        Repeater1.DataBind();
        Response.Redirect(string.Format("~/user/Autor.aspx?id={0}", Request.QueryString["id"]));
    }
}

我得到错误

'要插入的节点来自不同的文档上下文。'

当我尝试使用 replaceChild 方法时,它会引发。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    尝试替换此行: oldComment.ParentNode.ReplaceChild(doc.ImportNode(newComment, true), oldComment);

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2018-07-10
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多