XDocument can accept only limited content:
XElement object (the "root")XDeclaration objectXDocumentType object (to reference a DTD)XProcessingInstruction objectsXComment objectsThe simplest valid XDocument has just a root element:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
var doc = new XDocument(new XElement("test", "data"));
Console.WriteLine(doc);
}
}
The output:
data The next example produces an XHTML file, illustrating all the constructs that an XDocument can accept:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
var styleInstruction = new XProcessingInstruction("xml-stylesheet", "href='styles.css' type='text/css'");
var docType = new XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null);
XNamespace ns = "http://www.w3.org/1999/xhtml";
var root = new XElement(ns + "html", new XElement(ns + "head",
new XElement(ns + "title", "An XHTML page")), new XElement(ns + "body",
new XElement(ns + "p", "This is the content"))
);
var doc = new XDocument(
new XDeclaration("1.0", "utf-8", "no"), new XComment("Reference a stylesheet"), styleInstruction,
docType, root);
doc.Save("test.html");
Console.WriteLine(doc);
}
}
The output:
An XHTML page This is the content
| w___w_w___._j__a___v__a2___s_.___c_o___m_ | Contact Us |
| Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
| All other trademarks are property of their respective owners. |