Add a comment to XDocument in CSharp

Description

The following code shows how to add a comment to XDocument.

Example


using System;//w  w w  .  ja  va  2  s.  co  m
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));  
      // Add a comment.
      NewDoc.AddFirst(new XComment("This is a comment."));

      // Add another comment.
      NewDoc.Element("Root").Add(new XComment("This is another comment."));

      // Add a third comment.
      NewDoc.Add(new XComment("This is the third comment."));

      Console.WriteLine(NewDoc);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style