« VisioAutomation 2010 and VisioAutomation.VDX available as NuGet packages | Main | Visio C# Automation Samples with NuGet »
Monday
Jun252012

Programmatically Generating HTML5 in C# without the Xml Declaration and using the correct html DTD - using XmlWriter, XmlDocument, and XDocument  

Below a look at a simple “hello-world” example of HTML5:

<!DOCTYPE html>
<html>
  <head />
  <body>
    <p>Hello World</p>
  </body>
</html>

If you have ever generated HTML5 programmatically in C#, you may have noticed that by default you will get an unwanted Xml declaration (in red below) and the html DOCTYPE is not present.

Using System.Xml.XmlWriter

First, let's try using System.Xml.XmlWriter:

 var w = System.Xml.XmlWriter.Create("document_xmlwriter.htm");
w.WriteStartDocument();
w.WriteStartElement("html");
w.WriteStartElement(
"head");
w.WriteEndElement();
// </head>
w.WriteStartElement("body");
w.WriteStartElement(
"p");
w.WriteString(
"Hello World");
w.WriteEndElement();
// </p>
w.WriteEndElement(); // </body>
w.WriteEndElement(); // </html>
w.WriteEndDocument();
w.Close();

This code snippet will produce the following HTML. Note that I have manually indented the output to make it easier to read.

<?xml version="1.0" encoding="utf-8"?>
<html>
  <head />
  <body>
    <p>Hello World</p>
  </body>
</html>

Fortunately, it is very easy to alter the example to do exactly what we need. It merely involves using the XmlWriterSettings class to omit the Xml declaration and adding the doc type manually.

var settings = new System.Xml.XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

var w = System.Xml.XmlWriter.Create("document_xmlwriter.htm", settings);

w.WriteStartDocument();
w.WriteDocType("html", null, null, null);
w.WriteStartElement("html");
w.WriteStartElement("head");
w.WriteEndElement(); // </head>
w.WriteStartElement("body");
w.WriteStartElement("p");
w.WriteString("Hello World");
w.WriteEndElement(); // </p>
w.WriteEndElement(); // </body>
w.WriteEndElement(); // </html>
w.WriteEndDocument();
w.Close();

 

Using System.Xml.Linq.XDocument and XmlWriter

 

In order to achieve the same effect with System.Xml.Linq.XDocument we will need to use XmlWriter.

var d = new System.Xml.Linq.XDocument();
d.AddFirst(new  System.Xml.Linq.XDocumentType("html", null, null, null));

var html_el = new System.Xml.Linq.XElement("html");
d.Add(html_el);

var head_el = new System.Xml.Linq.XElement("head");
html_el.Add(head_el);

var body_el = new System.Xml.Linq.XElement("body");
html_el.Add(body_el);

var p_el = new System.Xml.Linq.XElement("p");
body_el.Add(p_el);

var text_el = new System.Xml.Linq.XText("Hello World");
p_el.Add(text_el);

var settings = new System.Xml.XmlWriterSettings();
settings.OmitXmlDeclaration = true;

var w = System.Xml.XmlWriter.Create("document_xdocument.htm", settings);
d.Save(w);
w.Close();

 

Using System.Xml.XmlDocument

Life is a little easier with System.Xml.XmlDocument because the Xml declaration is not output by default, so we only need to specify the DTD.

var d = new System.Xml.XmlDocument();

var dt = d.CreateDocumentType("html", null, null, null);
d.AppendChild(dt);

var html_el = d.CreateElement("html");
d.AppendChild(html_el);

var head_el = d.CreateElement("head");
html_el.AppendChild(head_el);

var body_el = d.CreateElement("body");
html_el.AppendChild(body_el);

var p_el = d.CreateElement("p");
body_el.AppendChild(p_el);

var text_el = d.CreateTextNode("Hello World");
p_el.AppendChild(text_el);

d.Save("document_xmldocument.htm");

 

 

 

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Thanks alot for this info. Helps alot!

April 8, 2013 | Unregistered Commenteraruna

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>