how to remove Xelement without its children node using LINQ?

c#, linq, linq-to-xml

Solution

var x = doc.Root.Element("X");
x.Remove();
doc.Root.Add(x.Elements());

Problem

Here is my `XML`, ``` <A> <B id="ABC"> <C name="A" /> <C name="B" /> </B> <X> <B id="ZYZ"> <C name="A" /> <C name="B" /> </B> </X> </A> ``` I'm using following code to remove `<X>` node without deleting its descents/childrens, ``` XDocument doc = XDocument.Load("D:\\parsedXml.xml"); doc.Descendants("A").Descendants("X").Remove(); ``` But is removing entire `<X>` block. Expected output : ``` <A> <B id="ABC"> <C name="A" /> <C name="B" /> </B> <B id="ZYZ"> <C name="A" /> <C name="B" /> </B> </A> ```

Original source