I have code that is supposed to either add an XmlElement to the root element of a document, or replace an existing element if there is one. Here is my code:
if (existingInfo != null)
{
existingInfo.ParentNode.ReplaceChild(existingInfo, newInfo);
}
else
{
this.rootElement.AppendChild(info)
}
configDocument.Save(this.filePath);
If I am appending a new item this is not a problem. However when I try to add a new item I get an ArgumentException stating “The node to be removed is not a child of this node”
This is a 2.0 application.
As stated in the docs, the first argument to
ReplaceChildmust be the new node, not the old one.Therefore, try: