Function RemoveChild(XmlNode node) failed in C#

Nano HE picture Nano HE · Jul 6, 2010 · Viewed 7k times · Source

When I try to remove some of my child element with RemoveChild(). But throw exception. I attached my code below.

    nodeName = doc.SelectSingleNode("//Equipment//DataCollections//EnabledIDs//MyID[@id='" + attrValue + "']"); 
   // Found the nodeName successfully druing run time.
    doc.DocumentElement.RemoveChild(nodeName); 
   // faild to Remove the node

Show error below:

An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dll

Additional information: The node to be removed is not a child of this node. 

How can I remove the node?

[Update]

VS2005 & .NET 2.0 used.

Answer

Trefex picture Trefex · Jul 6, 2010

I believe .RemoveChild is removing the child of the node you selected.

Are there any children under nodeName or is it the leaf already?

Edit:

Actually you need to remove the Child of the Parent, try the following:

nodeName.parentNode.removeChild(nodeName)