Add to XDocument

In this chapter you will learn:

  1. How to add to the first of XDocument
  2. Add the CData to XDocument

Add to the first

using System;//from   j  a v a 2  s  .c o  m
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));  
      NewDoc.AddFirst(new XDocumentType(
               "MyDocType", "MyPubID", 
               "MySysID", "ThisInternalSubset"));


   }
}

Add the CData to XDocument

using System;//from   ja v  a2  s.  c  o m
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


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 the CData.
      NewDoc.Element("Root").Add(
            new XElement("CDataElement",
               new XCData("This is a CData Entry!")));

   }
}

Next chapter...

What you will learn in the next chapter:

  1. How to load XML file with XDocument
  2. Loading a Document with the XDocument.Load Method with LoadOptions
Home » C# Tutorial » XML Linq
XDocument
Create XDocument
Add to XDocument
Parse XML file with XDocument
Load XML string with XDocument
XDocument Root
Query XML document with Linq
Save XML document
XDocument Serialize
XElement namespace
Adding attribute
XElement's NextNode
XAttribute
XAttribute value
XAttribute Properties
XAttribute namespace
XComment
XDeclaration
XML declaration
XCData
XNamespace