1

I'm creating an ASP.NET server control using the HtmlTextWriter class. My understanding is that it's a nice class to use for making sure my output HTML is valid. I assumed it would format things nicely as well, but it does a bunch of strange stuff that makes the output hard to read.

Are there some settings or something I can play with to get this looking like someone took the time to properly format it, or is it just a downside of using this class? Here are some examples of the ugly formatting I'm talking about:

  • Inconsistent use of self-closing tags. With some tags I get them, and others I don't.
  • Random newlines between tags.
  • Lack of newlines in appropriate places.
  • Mismatched indentation.

This is actually what I'm trying to reproduce:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="MySWF" width="100" height="100" codebase="https://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="/MySWF.swf"></param>
    <param name="quality" value="high" />
    <param name="allowScriptAccess" value="sameDomain" /> 
    <embed align="middle" pluginspage="https://www.adobe.com/go/getflashplayer" width="100" quality="high" height="100" loop="false" name="MySWF" type="application/x-shockwave-flash" play="true" allowscriptaccess="sameDomain" src="/MySWF.swf" />
</object>

...and this is what I'm getting:

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="MySWF" width="100" height="100" codebase="https://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
        <param name="movie" value="/MySWF.swf">

        </param><param name="quality" value="high">

        </param><param name="allowScriptAccess" value="sameDomain">

        </param><embed align="middle" pluginspage="https://www.adobe.com/go/getflashplayer" width="100" quality="high" height="100" loop="false" name="MySWF" type="application/x-shockwave-flash" play="true" allowscriptaccess="sameDomain" src="/MySWF.swf" />
</object>
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
  • there is a very similar question here, see if the answers can help: http://stackoverflow.com/questions/2172174/need-tool-to-format-html-indent-add-whitespace – Paolo Falabella Feb 22 '11 at 14:33
  • I don't want to detract from your question but is it *really* important how the markup is formatted? Isn't the important thing that the markup is *correct*? When you want to read the source perhaps you should use something like Firebug or Chrome's Developer Tools which with reformat the code for you. – Andrew Hare Feb 22 '11 at 14:33
  • @Andrew Hare: In this case, not really since it's such a small control. In other cases where the control might be complex, it makes it easier for other developers or users who want to check out the source to see without requiring extra tools to do so. – Ocelot20 Feb 22 '11 at 14:45

1 Answers1

2

AFAIK, there aren't any settings for the actual formatting. If you want to format it yourself, that would probably be the best solution. Thou, this would create some overhead, so idk if it's worth it. Here are some open source examples of DIY formatting

http://snipplr.com/view/28048/net-html-formatter/

http://weblogs.asp.net/scottcate/archive/2007/01/10/my-c-code-formatting.aspx

joe_coolish
  • 7,201
  • 13
  • 64
  • 111