CSharp - Removing a Node's Content with RemoveAll

Description

Removing a Node's Content with RemoveAll

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

class Program//from w  ww.  j a  v a 2  s. c om
{
    static void Main(string[] args){
          XDocument xDocument = new XDocument(
            new XElement("Books",
              new XElement("Book",
                new XAttribute("type", "Author"),
                new XElement("FirstName", "Joe"),
                new XElement("LastName", "Ruby")),
              new XElement("Book",
                new XAttribute("type", "Editor"),
                new XElement("FirstName", "PHP"),
                new XElement("LastName", "Python"))));
    
          Console.WriteLine(System.Environment.NewLine + "Before removing the content.");
          Console.WriteLine(xDocument);
    
          xDocument.Element("Books").RemoveAll();
    
          Console.WriteLine(System.Environment.NewLine + "After removing the content.");
          Console.WriteLine(xDocument);
    }
}

Result

Related Topic