是否有使用的HtmlTextWriter如果你不打算从自适应渲染中获益什么好处? [英] Are there any benefits to using HtmlTextWriter if you are not going to benefit from adaptive rendering?

查看:226
本文介绍了是否有使用的HtmlTextWriter如果你不打算从自适应渲染中获益什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Adaptive渲染备用设备中受益之外,它曾经是有意义的编写所有这些代码:



  writer.WriteBeginTag(表); 
writer.WriteBeginTag(TR);
writer.WriteBeginTag(TD);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEncodedText(someTextVariable);
writer.WriteEndTag(TD);
writer.WriteEndTag(TR);
writer.WriteEndTag(表);



当StringBuilder的可以建立同样的事情简单:

  sb.Append(<表>< TR>< TD>中); 
sb.Append(someTextVariable);
sb.Append(< / TD>< / TR>< /表>);


解决方案

我能想到的两个原因使用的HtmlTextWriter:一),可以使用作家,让您的缩进的轨道,让你的输出HTML是很好的格式,而不是出现在一行和b)的HtmlTextWriter通常与一个输出流相关的,所以它应该比建立一个更有效在内存中长字符串(取决于你有多少HTML生成)。



这些都不是非凡的原因,但他们有足够说服我的时候使用效率作家是必要的,如果我写,将被重用,并应尽可能专业基地的控制。你的情况可能会有所不同: - )


Outside of benefiting from Adaptive Rendering for alternate devices, does it ever make sense to write all of this code:

writer.WriteBeginTag("table");
writer.WriteBeginTag("tr");
writer.WriteBeginTag("td");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEncodedText(someTextVariable);
writer.WriteEndTag("td");
writer.WriteEndTag("tr");
writer.WriteEndTag("table");

When StringBuilder could build the same thing with simply this:

sb.Append("<table><tr><td>");
sb.Append(someTextVariable);
sb.Append("</td></tr></table>");

解决方案

I can think of two reasons to use HtmlTextWriter: a) you can use the writer to keep track of your indents, so that your outputted HTML is formatted nicely, rather than appearing on one line and b) HtmlTextWriter is usually associated with an output stream, so it should be more efficient than building up a long string in memory (depending upon how much HTML you are generating).

Neither of these are extraordinary reasons, but they are enough to convince me to use the writer when efficiency is needed, or if I am writing a base control that will be reused and should be as professional as possible. Your mileage may vary :-).

这篇关于是否有使用的HtmlTextWriter如果你不打算从自适应渲染中获益什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆