4

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.

Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • It seems that the child node, when selected, is not "related" to the its parent instance from which it was created. This seems like a bug to me. The fix is to do (as others said): `childNode.ParentNode.RemoveChild(childNode)` – Josh M. Jul 10 '12 at 13:51

4 Answers4

9

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)
Trefex
  • 2,290
  • 16
  • 18
5

You're trying to remove a node directly from the document element, when it's actually a great-grandchild of the document element (or maybe just a grandchild). RemoveChild only works when you specify a direct child, not just any descendant.

Try this:

nodeName.ParentNode.RemoveChild(nodeName);

(If Remove() exists as per Adkins' answer, that would be better - but I can't find such a method in MSDN.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • it does exist if you are using Linq to XML. I expanded a bit on my answer to cover it. – Christopher B. Adkins Jul 06 '10 at 09:18
  • I read another booked named "Pro .NET 2.0 XML". I followed the sample code form to write in my original code. But the sample xml file is not complex like mine. As Jon' answer. My element should be the great-grandchild of the document element. +1 & Thank you. – Nano HE Jul 06 '10 at 09:29
  • @Adkins. I added a protection like `if (node != null) { // do ...}` – Nano HE Jul 06 '10 at 09:32
3

You should remove child from the immediate parent, not from the top:

nodeName.ParentNode.RemoveChild(nodeName); 
unbeli
  • 29,501
  • 5
  • 55
  • 57
1

Instead of using .RemoveChild try just using .Remove That should give you the outcome you are looking for.

Edit::

Note that this only works if you are using Linq to XML. Then you would be working with some variation of an XNode and can simply say blah.Remove and it should do the trick. If you are not using Linq to XML I would suggest looking into that cause it is amazing.

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29
  • There is no Remove() method based on my VS2005 & .NET 2.0. :-) – Nano HE Jul 06 '10 at 09:21
  • @Nano: Linq first came with .NET 3.5(?) so that could be why my answer doesn't work for you. Do you have certain requirements that limit you to 2.0? If not it could be worth looking into upgrading. – Christopher B. Adkins Jul 06 '10 at 09:31