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

C# 要删除的节点不是此节点的子节点,c#,xml,c#-2.0,C#,Xml,C# 2.0,我有一段代码,可以向文档的根元素添加一个元素,或者替换现有元素(如果有)。这是我的密码: if (existingInfo != null) { existingInfo.ParentNode.ReplaceChild(existingInfo, newInfo); } else { this.rootElement.AppendChild(info) } configDocument.Save(this.filePath); 如果我要添加一个新项目,这不是问题。但是,当我尝试

我有一段代码,可以向文档的根元素添加一个元素,或者替换现有元素(如果有)。这是我的密码:

if (existingInfo != null)
{
    existingInfo.ParentNode.ReplaceChild(existingInfo, newInfo);
}
else
{
    this.rootElement.AppendChild(info)
}
configDocument.Save(this.filePath);
如果我要添加一个新项目,这不是问题。但是,当我尝试添加一个新项目时,我得到一个声明“要删除的节点不是此节点的子节点”

这是一个2.0应用程序。

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

因此,请尝试:

existingInfo.ParentNode.ReplaceChild(newInfo, existingInfo);

总是一些小事。我现在不能测试这个,但我把它作为答案,因为我相信这是我的错误。