HTMLWriter 1.1

A few days ago I published the original HTMLWriter article. Since then I’ve added a few methods to make simple formatting easier. This includes a way to specify an ‘indentation’ level. Right now it is pretty basic but it helps making some html tags like tables easier to read.

The most important methods added are these:

private void AppendInternalCRLF()

lastWrittenIndentation = false;


private void AppendInternalIndentation()

sbHtmlContent.Append(new string(‘\t’, Indentation));
lastWrittenIndentation = true;


public HTMLWriter AppendNewLine()

return this;

public HTMLWriter AppendNewLineWithIndentation()

if (AutoFormatting && !lastWrittenIndentation)

return this.AppendNewLine().AppendIndentation();


return this;


The lastWrittenIndentation variable is there since some html tags can call this method at the beginning and the end. If the next tag also starts by calling the method again then it ignores the method to avoid duplicating the indentation.

An example of one of the methods using it:

public HTMLWriter AppendTableStart(string className, params CustomAttribute[] customAttributes)

return AppendTagStart(“table”, className, customAttributes);


Then I played around trying to ‘Normalize’ the html by using a library I discovered on CodePlex a while ago – System.Html

There seems to be a little issue using this library of the html is condensed (i.e. no CRLFs) but that is solved by cheating and replacing all ‘><‘ characters with ‘>\r\n<‘ .

Please keep in mind that this library was created to create simple html fragments or documents – mainly to generate reports inside a normal Winforms application.

The updated library can be found here.



  1. HTMLWriter 1.2 | Rudolf's mind - pingback on 2011/05/05 at 22:59

Leave a Reply

Trackbacks and Pingbacks:

%d bloggers like this: