Skip to content
chrcar01 edited this page Sep 13, 2010 · 7 revisions

HtmlBuilder

What Is It?

A well documented Fluent .NET API for creating html.

How Do I Use It?

Download the assembly from the downloads page. Reference it in your project. Make sure to import the HtmlBuilder namespace and you’re ready to go.

Some simple markup:

new Element("p").Update("Hello World");

// <p>Hello World</p>

Add styles, attributes, and css classes as one string:

new Element("span","style=font-weight:bold;width=200px;class=sooper;")

// <span width="200px" style="font-weight:bold;" class="sooper"></span>

Nested elements:

new Element("span",
	new Element("b").Update("Hello"));

// <span><b>Hello</b></span>

Strings and Elements are interchangeable:

new Element("b").Update("hello") + "<i>bye bye</i>";

// <b>hello</b><i>bye bye</i>
public string GetSomeHtml(){
  return new Element("p").Update("Here's some html");
}

string html = GetSomeHtml();

// <p>Here's some html</p>

Render to anything:XmlWriter, HtmlTextWriter, TextWriter, anything that supports Stream.

string path = @"C:\htmlbuilder-test.html";
using (FileStream file = new FileStream(path, FileMode.CreateNew))
{
	new Element("html",
		new Element("body",
			new Element("h1").Update("Hello World")
		)
	).Render(file);
}