【问题标题】:The node to be removed is not a child of this node要移除的节点不是该节点的子节点
【发布时间】:2012-12-13 20:57:19
【问题描述】:

我的代码应该将XmlElement 添加到文档的根元素,或者替换现有元素(如果有的话)。这是我的代码:

if (existingInfo != null)
{
    existingInfo.ParentNode.ReplaceChild(existingInfo, newInfo);
}
else
{
    this.rootElement.AppendChild(info)
}
configDocument.Save(this.filePath);

如果我要附加一个新项目,这不是问题。但是,当我尝试添加一个新项目时,我收到一个 ArgumentException 声明“要删除的节点不是该节点的子节点”

这是一个 2.0 应用程序。

【问题讨论】:

    标签: c# xml c#-2.0


    【解决方案1】:

    in the docs 所述,ReplaceChild 的第一个参数必须是新节点,而不是旧节点。

    因此,请尝试:

    existingInfo.ParentNode.ReplaceChild(newInfo, existingInfo);
    

    【讨论】:

    • 总是小事。我现在无法对此进行测试,但我将其标记为答案,因为我确信这是我的错误。
    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2022-11-12
    • 2019-06-17
    相关资源
    最近更新 更多