Question

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?

Was it helpful?

Solution

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, "    ");

OTHER TIPS

David has technically answered your question, but that path leads to the dark side. Client-side web code is not compiled. If you use four spaces instead of a tab character, you're doing it wrong.

Even if the impact is small, when is it acceptable to be 400% less efficient? If you want to make things a tiny bit slower for no good reason, you could also randomly add Thread.Sleep() calls to your server-side code.

Fix your text editor's settings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top