tags:

views:

464

answers:

1

I am creating some html from code running on a WinForm. I want to create the following html:

<html>
<body>
    <div>
        <p>foo</p>
    </div>
</body>
</html>

I'm using the System.Web.UI.HtmlTextWriter class to do this like so:

System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter();
htmlWriter.WriteFullBeginTag("html");
htmlWriter.WriteLine();
htmlWriter.WriteFullBeginTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent++;
htmlWriter.WriteFullBeginTag("div");
htmlWriter.WriteLine();
htmlWriter.Indent++;
htmlWriter.WriteFullBeginTag("p");
htmlWriter.Write("foo");
htmlWriter.WriteEndTag("p");
htmlWriter.Indent--;
htmlWriter.WriteLine();
htmlWriter.WriteEndTag("div");    
htmlWriter.Indent--;
htmlWriter.WriteLine();
htmlWriter.WriteEndTag("body");
htmlWriter.WriteLine();
htmlWriter.WriteEndTag("html");

This works just fine, however, when I open the resulting html in a text editor my indentation just looks way too far to me, since it is using tab chars. Is there a way to force this class to use a number of spaces instead?

+4  A: 

I'm not a System.Web.UI guy, but from the MSDN documentation, it looks like you can supply the character to use for indentation as the second argument to the HtmlTextWriter constructor. Thus, to get 4 spaces instead of a tab:

System.Web.UI.HtmlTextWriter htmlWriter = 
    new System.Web.UI.HtmlTextWriter(yourTextWriter, "    ");
David Brown
Wow... I feel like a mouth breather. Thanks!
Zemm