Home
C#
HtmlTextWriter Example
This page was last reviewed on Jan 10, 2022.
Dot Net Perls
HtmlTextWriter writes HTML. It allows you to generate a list of HTML elements, such as div elements. You could use StringBuilder, but HtmlTextWriter is sometimes better.
Notes, HTML. It is easy to generate HTML using StringBuilder, just like any other string. That approach can result in complex syntax and syntax errors.
StringBuilder
String Remove HTML
HtmlEncode, HtmlDecode
Example. First, the HtmlTextWriter class is found in the System.Web.UI namespace. You may need to reference the System.Web assembly. You also need the System.IO namespace for StringWriter.
Detail The new StringWriter is required for the HtmlTextWriter to write to. It is a buffer and everything that is written is written to this.
StringWriter
Detail We need to use "RenderBegin" and "RenderEnd" methods. Call RenderBeginTag to open a new level in the HTML with that tag.
And You must manually close that level of that markup with RenderEndTag. These 2 methods must be called in pairs.
Detail Add the attribute with AddAttribute before BeginRenderTag. Look for "HtmlTextWriterAttribute.Class" and similar.
using System; using System.IO; using System.Web.UI; class Program { static string[] _words = { "Sam", "Dot", "Perls" }; static string GetDivElements() { // Initialize StringWriter instance. StringWriter stringWriter = new StringWriter(); // Put HtmlTextWriter in using block because it needs to call Dispose. using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)) { // Loop over some strings. foreach (var word in _words) { // Some strings for the attributes. string classValue = "ClassName"; string urlValue = "http://www.dotnetperls.com/"; string imageValue = "image.jpg"; // The important part: writer.AddAttribute(HtmlTextWriterAttribute.Class, classValue); writer.RenderBeginTag(HtmlTextWriterTag.Div); // Begin #1 writer.AddAttribute(HtmlTextWriterAttribute.Href, urlValue); writer.RenderBeginTag(HtmlTextWriterTag.A); // Begin #2 writer.AddAttribute(HtmlTextWriterAttribute.Src, imageValue); writer.AddAttribute(HtmlTextWriterAttribute.Width, "60"); writer.AddAttribute(HtmlTextWriterAttribute.Height, "60"); writer.AddAttribute(HtmlTextWriterAttribute.Alt, ""); writer.RenderBeginTag(HtmlTextWriterTag.Img); // Begin #3 writer.RenderEndTag(); // End #3 writer.Write(word); writer.RenderEndTag(); // End #2 writer.RenderEndTag(); // End #1 } } // Return the result. return stringWriter.ToString(); } static void Main() { // Demonstrate HtmlTextWriter. Console.WriteLine(GetDivElements()); } }
<div class="ClassName"> <a href=&quot;http://www.dotnetperls.com/&quot;> <img src="image.jpg" width="60" height="60" alt=&quot;&quot; />Sam</a> </div><div class="ClassName"> <a href=&quot;http://www.dotnetperls.com/&quot;> <img src="image.jpg" width="60" height="60" alt=&quot;&quot; />Dot</a> </div><div class="ClassName"> <a href=&quot;http://www.dotnetperls.com/&quot;> <img src="image.jpg" width="60" height="60" alt=&quot;&quot; />Perls</a> </div>
Tab string. In the constructor to the HtmlTextWriter, use an empty string as the tab string. This will eliminate a lot of whitespace—it can reduce page size.
string.Empty
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter, string.Empty)) { // Insert code here. }
Notes, tags. In the code, every tag is started and then closed later. The Write() method is an instance method that writes text in between the tags in HTML.
XmlWriter. Using XmlWriter is similar, but the output is XML. This is useful for interoperability and settings files. HtmlTextWriter is mainly useful for web pages in ASP.NET.
XmlWriter
A summary. We used the HtmlTextWriter class in the System.Web namespace. Use HtmlTextWriter to write HTML instead of StringBuilder alone for the cleanest and object-oriented code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 10, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.