Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any benefits to using HtmlTextWriter if you are not going to benefit from adaptive rendering?

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>");
like image 946
Chris Ballance Avatar asked Feb 21 '09 20:02

Chris Ballance


3 Answers

Another advantage could be that using HtmlTextWriter one could format code in a cleaner (more maintenance friendly) way, and that HtmlTextWriter supports encoding HTML automatically. Compare:

writer.AddAttribute(HtmlTextWriterAttribute.Id, "someId");
if (!string.IsNullOrEmpty(cssClass)) writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.WriteEncodedText(text);
writer.RenderEndTag();

versus:

StringBuilder html = new StringBuilder();
html.Append("<span");
html.Append(" id=\"someId\"");
if (!string.IsNullOrEmpty(cssClass)) html.AppendFormat(" class=\"{0}\"", HttpUtility.HtmlAttributeEncode(cssClass));
html.Append(">");
html.Append(HttpUtility.HtmlEncode(text));
html.Append("</span>");

One may argue that the code in the second example can be written in a different, possibly cleaner, way, but this could be seen as an advantage of HtmlTextWriter because it basically enforces one canonical way of formatting (which again improves maintenance).

Edit: In fact, I actually made a mistake in the second snippet, and I needed to go back and fix the response. This confirms the point I wanted to make.

like image 174
Jan Zich Avatar answered Oct 03 '22 19:10

Jan Zich


I can think of two reasons to use HtmlTextWriter:

  1. 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

  2. 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 :-).

like image 33
tsimon Avatar answered Oct 03 '22 17:10

tsimon


HtmlTextWriter is beneficial because:

HtmlTextWriter is the cleanest and the mark-up is nicely indented when it is rendered.

There is a performance impact as HtmlTextWriter writes directly to the output stream. Stringbuilder doesn't write to the output stream until ToString is called on it.

There is an example on why you would use HtmlTextWriter for Saving and Reusing HTML Output here as well.

like image 40
cgreeno Avatar answered Oct 03 '22 18:10

cgreeno