how to remove Xelement without its children node using LINQ?

后端 未结 4 944
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 02:49

Here is my XML,


    
      
           
    
    
         


        
4条回答
  •  不思量自难忘°
    2021-01-14 03:38

    Approved answer will always add children to the end of document. If you need to remove entry in the middle of the document and keep children where they sit, do following:

    x.AddAfterSelf(x.Nodes());
    x.Remove();
    

    Following code removes all nodes keeping children in the correct place:

    while (doc.Descendants("x").Count() > 0)
    {
        var x = doc.Descendants("x").First();
        x.AddAfterSelf(x.Nodes());
        x.Remove();
    }
    

提交回复
热议问题