如何使 HtmlTextWriter 对象使用空格而不是制表符作为其缩进?
我正在从 WinForm 上运行的代码创建一些 html。 我想创建以下 html:
<html>
<body>
<div>
<p>foo</p>
</div>
</body>
</html>
我正在使用 System.Web.UI.HtmlTextWriter 类来执行此操作,如下所示:
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");
这工作得很好,但是,当我在文本编辑器中打开生成的 html 时,我的缩进看起来也太糟糕了对我来说很远,因为它使用制表符。 有没有办法强制此类使用多个空格?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 System.Web.UI 人员,而是来自 MSDN 文档,看起来您可以提供用于缩进的字符作为 HtmlTextWriter 构造函数的第二个参数。 因此,要获得 4 个空格而不是制表符:
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:
大卫从技术上已经回答了你的问题,但这条路通向黑暗面。 客户端 Web 代码未编译。 如果您使用四个空格而不是制表符,那么您就做错了。
即使影响很小,什么时候可以接受效率降低 400%? 如果您无缘无故地想让事情变慢一点,您也可以随机添加 Thread.Sleep() 调用到您的服务器端代码。
修复文本编辑器的设置。
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.