解决 HtmlTextWriter 丑陋的格式?
我正在使用 HtmlTextWriter 类创建 ASP.NET 服务器控件。我的理解是,它是一个很好的类,可以用来确保我的输出 HTML 有效。我以为它也能很好地格式化内容,但它做了一些奇怪的事情,使输出难以阅读。
是否有一些设置或我可以使用的东西来让它看起来像有人花时间正确格式化它,或者这只是使用此类的一个缺点?以下是我正在讨论的丑陋格式的一些示例:
- 自闭标签的使用不一致。对于某些标签,我得到了它们,而另一些则没有。
- 标签之间的随机换行符。
- 在适当的地方缺少换行符。
- 缩进不匹配。
这实际上就是我想要重现的:
<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>
...这就是我得到的:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK,没有任何实际格式设置。如果您想自己格式化它,这可能是最好的解决方案。你,这会产生一些开销,所以不知道是否值得。以下是一些 DIY 格式化的开源示例
http://snipplr.com/view/28048 /net-html-formatter/
http://weblogs.asp.net/scottcate/archive/2007/01/10/my-c-code-formatting.aspx
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