2012-12-13 143 views
1

我的代码应该是将XmlElement添加到文档的根元素,或者替换现有元素(如果有)。这里是我的代码:要删除的节点不是此节点的子节点

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

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

这是一个2.0应用程序。

回答

5

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

因此,尝试:

existingInfo.ParentNode.ReplaceChild(newInfo, existingInfo); 
+0

它总是小事情。我现在无法测试,但我将其标记为答案,因为我确信这是我的错误。 – Fr33dan