An approved answer always adds children to the end of the document. If you need to delete the entry in the middle of the document and leave the children where they are sitting, follow these steps:
x.AddAfterSelf(x.Nodes());
x.Remove();
The following code deletes all nodes <x>, keeping the children in the right place:
while (doc.Descendants("x").Count() > 0)
{
var x = doc.Descendants("x").First();
x.AddAfterSelf(x.Nodes());
x.Remove();
}
source
share