CSharp - Remove a sequence of the comments using Remove operator

Description

Remove a sequence of the comments using Remove operator

Demo

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

class Program/*from  w w w.  j  a v  a2  s .c  om*/
{
    static void Main(string[] args){
          XDocument xDocument = new XDocument(
            new XElement("Books",
              new XElement("Book",
                new XAttribute("type", "Author"),
                new XComment("This is a new author."),
                new XElement("FirstName", "Joe"),
                new XElement("LastName", "Ruby")),
              new XElement("Book",
                new XAttribute("type", "Editor"),
                new XElement("FirstName", "PHP"),
                new XElement("LastName", "Python"))));
    
          IEnumerable<XComment> comments =
            xDocument.Element("Books").Elements("Book").
              Nodes().OfType<XComment>();
    
          //  First, we will display the source comments.
          foreach (XComment comment in comments)
          {
            Console.WriteLine("Source comment: {0}", comment);
          }
    
          comments.Remove();
    
          //  Now, we will display the XML document.
          Console.WriteLine(xDocument);
    }
}

Result

Related Topic