About HTMLWriter

This little utility library was created to generate raw html fragments or documents that could be used inside other applications for displaying things like reports or provide a means to have printing functionality etc.
It is a very lightweight library and based on the .Net StringBuilder class - thus very fast and optimized for creating large html fragments if needed.

Features

The HTMLWriter class allows you to build up the html fragment tag (html tag) by tag and optionally to specify html attributes.

Examples

// Usage example

            HTMLWriter h = new HTMLWriter("Test", true);
            h.AppendStyle("body", "font-family:Arial;");
            h.AppendStyle("mycssclass", "font-family:Verdana;");
            h.AppendHeading(1, "Heading 1");
            h.AppendHorisontalLine();
            h.AppendBlockquoteStart()
                .AppendParagraph("Hello paragraph", "mycssclass")
                .AppendUnOrderedListStart("mylist")
                    .AppendListItem("Item 1")
                    .AppendListItem("Item 2")
                    .AppendListItem("Item 3")
                .AppendTagEnd(2); //blockquote
            h.AppendTableStart("main", new CustomAttribute("border", "0"))
                .AppendTableHeadStart()
                .AppendTableRowStart()
                .AppendTableCellStart("", new CustomAttribute("colspan", "2"))
                .AppendText("Header")
                .AppendTagEnd(3) //cell, row and thead
                .AppendTableFootStart()
                .AppendTableRowStart()
                .AppendTableCell("Footer", "", new CustomAttribute("colspan", "2"))
                .AppendTagEnd(2) //row and tfoot
                .AppendTableRowStart()
                .AppendTableHeaderCellStart()
                .AppendText("Name")
                .AppendTagEnd() //cell
                .AppendTableHeaderCellStart()
                .AppendText("Surname")
                .AppendTagEnd(2) //cell, row
                .AppendTableRowStart()
                .AppendTableCell("Luke")
                .AppendTableCell("Skywalker")
                .AppendAllEndTagsUntil("table"); //cell, row, table
            Console.WriteLine(h.ToString());


The output would look like this:

<html>
<head>
<title>Test</title>
<style type="text/css">
body
{font-family:Arial;}
mycssclass
{font-family:Verdana;}

</style>
</head>
<body>
<h1>Heading 1</h1>
<hr />
<blockquote>
  <p class="mycssclass">Hello paragraph</p>
  <ul class="mylist">
    <li>Item 1</li><li>Item 2</li><li>Item 3</li>
  </ul>
</blockquote>
<table class="main" border="0">
  <thead>
    <tr>
      <td colspan="2">Header</td>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td colspan="2">Footer</td>
    </tr>
  </tfoot>
  <tr>
    <th>Name</th>
    <th>Surname</th>
  </tr>
  <tr>
    <td>Luke</td>
    <td>Skywalker</td>
  </tr>
</table>

</body>
</html>


For more examples how to use the library see the source code as hosted on CodePlex or have a look at the original blog entries:

Comment and suggestions

Comments and suggestion for more extensions are welcome - but keep in mind this was and is suppose to be a 'lightweight' utility library :)