要删除的节点不是此节点的子节点

时间:2012-12-13 20:57:19

标签: c# xml c#-2.0

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

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

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

这是一个2.0应用程序。

1 个答案:

答案 0 :(得分:7)

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

因此,请尝试:

existingInfo.ParentNode.ReplaceChild(newInfo, existingInfo);
相关问题