Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove Xelement without its children node using LINQ?

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>
like image 861
user2729272 Avatar asked Jan 12 '23 05:01

user2729272


1 Answers

var x = doc.Root.Element("X");
x.Remove();
doc.Root.Add(x.Elements());
like image 154
L.B Avatar answered Feb 02 '23 09:02

L.B