This project is read-only.

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.

  • Some tags have predefined attributes that almost always has to be specified - like <a> has the url attribute, so the methods to add the anchor tag all the url as a parameter. Another common attribute on most tags is the class name (CSS class name). Most methods have multiple overloads to make it easier to use.
  • There are also a couple of helper classes - one to help with custom html attributes and another to help with DataTable formatting.
  • There is a set of extension methods to facilitate passing a whole DataTable (like in DataSets) in and have a whole html table generated based on the content - complete with alternating rows and even an 'editing page link' if needed.
  • It supports some limited html formatting functionality. Some tags can automatically be placed on a new line and the indentation level changed - like 'blockquote' or 'tr'.
  • Stylesheet definitions (all the stuff between <style> and </style>) are handled separately and only get merged when the final fragment/document is generated. Both internal and external stylesheets are supported.
  • It has a built-in method for saving the generated html to a file - making it easier to use inside a WebBrowser control. It does not force you what directory to use - although it is recommended to use a user/data directory e.g. System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
  • It is easily extendable - you can easily add more html tags or more overloads to meet your needs. The main HTMLWriter and base class are separated so that you can even create your own html or other markup language writer.
  • It provides static methods for getting or escaping html special characters.

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 :)

Last edited Mar 8, 2012 at 7:15 AM by RudolfHenning, version 14