Outside of benefiting from Adaptive Rendering for alternate devices, does it ever make sense to write all of this code:
writer.WriteBeginTag(\"t
I can think of two reasons to use HtmlTextWriter:
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
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 :-).
The biggest reason I can think of is to avoid having to take an extra step to sanitize your input. According to the docs, WriteEncodedText will automatically format any angle brackets appropriately.
XSS is a real thing, and anything you can do to make it easier for future devs to maintain your code is a benefit.
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.
At some point, you still need to pass this to a HtmlTextWriter in order to render to the client. I guess you would have a final writer.Write(sb.ToString()); in there in the second example. What you can do to reduce the lines of code is writing raw HTML, exactly the same way as in your second StringBuilder example, but using HtmlTextWriter.Write instead.
writer.Write("<table><tr><td>");
writer.Write(someTextVariable);
writer.Write("</td></tr></table>");
Then using a StringBuilder seems unnecessary. And, HtmlTextWriter will, at least to some extent, make sure the generated HTML is compliant(although that is not true in the above case, when writing raw HTML).
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.