CSharp - Creating Documents with XDocument

Introduction

XML documents are implemented with the XDocument class.

Demo

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

class Program//from  w  ww . ja v a  2  s. c om
{
    static void Main(string[] args){
      XDocument xDocument = new XDocument();
      Console.WriteLine(xDocument);

      xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XDocumentType("Books", null, "Books.dtd", null),
        new XProcessingInstruction("BookCataloger", "out-of-print"),
        new XElement("Books"));

      Console.WriteLine(xDocument);
    }
}

Result

The document's ToString method omits the declaration from its output.

If you debug the code and examine the document, you will see that the declaration is there.

Related Topics