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

5

15

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>");
Kosak answered 21/2, 2009 at 20:49 Comment(1)
See here #490191Hepatic
B
11

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

Basilbasilar answered 21/2, 2009 at 21:3 Comment(1)
hi Can u please help, I want to render the HTmltextwriter output on the Outlook Email Template on a new window.when I tried the HTML tags are not properly rendering <43><41></41><12><25>Hello world!!!</25></12></43> ...the Follwing numbers should not appear..please suggestOutvote
A
12

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.

Assembly answered 7/4, 2009 at 6:36 Comment(0)
B
11

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

Basilbasilar answered 21/2, 2009 at 21:3 Comment(1)
hi Can u please help, I want to render the HTmltextwriter output on the Outlook Email Template on a new window.when I tried the HTML tags are not properly rendering <43><41></41><12><25>Hello world!!!</25></12></43> ...the Follwing numbers should not appear..please suggestOutvote
R
6

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.

Rileyrilievo answered 21/2, 2009 at 21:14 Comment(1)
hi,I want to render the HTmltextwriter output on the Outlook Email Template on a new window.when I tried the HTML tags are not properly rendering <43><41></41><12><25>Hello world!!!</25></12></43>Outvote
E
2

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

Exhibitioner answered 21/2, 2009 at 21:2 Comment(0)
W
1

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.

Weksler answered 5/3, 2019 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.