C# HtmlTextWriter Example

C# HtmlTextWriter Example

 HtmlTextWriter Table Example.HtmlTextWriter is the efficient way of generating Html output instead of concatenating the string or using StringBuilder. Here we will see C#.net and VB.net examples of HtmlTextWriter. HtmlTextWriter class is located in the System.Web.UI namespace. You also required System.IO namespace to use StringWriter.

You may want to use HtmlTextWriter in your class library project. To do that, add a reference of System.Web assembly to your class library project.

C# HtmlTextWriter Table Example

Here we will generate the table output using C# and VB.net HtmlTextWriter. The constructor of HtmlTextWriter accepts String writer as a parameter. StrinWriter is required instance to work. HtmlTextWriter.TagRightChar generates ">" closing right bracket character. You can also add required attributes like class name or ID etc. before TagRightChar.

HtmlTextWriter Example.
using System;
using System.IO;
using System.Web.UI;

namespace Csharp.Html_Text_Writer.Example
{
  class Program
  {
    static void Main(string[] args)
    {
    
    StringWriter stringwriter = new StringWriter();
    
    HtmlTextWriter writer = new HtmlTextWriter(stringwriter);
    
    writer.WriteBeginTag("p");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("Dear Customer, here is Montly Report of Products.");
    writer.WriteEndTag("p");
    
    
    writer.WriteBeginTag("table");
    writer.Write(HtmlTextWriter.TagRightChar);
    
    writer.WriteBeginTag("tr");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("Product Name");
    writer.WriteEndTag("td");

    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("Quantity");
    writer.WriteEndTag("td");
    

    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("Price");
    writer.WriteEndTag("td");
    
    writer.WriteEndTag("tr");
    writer.WriteBeginTag("tr");
    
    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("Samsung Android");
    writer.WriteEndTag("td");

    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write("5");
    writer.WriteEndTag("td");

    writer.WriteBeginTag("td");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write(10750.51);
    writer.WriteEndTag("td");

    writer.WriteEndTag("tr");
    
    writer.WriteEndTag("table");
    
 
    
    Console.WriteLine(stringwriter.ToString());
    
    
     
     Console.ReadLine();

    }
  }
}
Output
<p>Dear Customer, here is Montly Report of Products.</p><table><tr><td>Product N
ame</td><td>Quantity</td><td>Price</td></tr><tr<td>Samsung Android</td><td>5</td
><td>10750.51</td></tr></table>
VB.net HtmlTextWriter Example.
Imports System
Imports System.IO
Imports System.Web.UI

Namespace Csharp.Html_Text_Writer.Example
	Class Program
		Private Shared Sub Main(args As String())

			Dim stringwriter As New StringWriter()

			Dim writer As New HtmlTextWriter(stringwriter)

			writer.WriteBeginTag("p")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("Dear Customer, here is Montly Report of Products.")
			writer.WriteEndTag("p")


			writer.WriteBeginTag("table")
			writer.Write(HtmlTextWriter.TagRightChar)

			writer.WriteBeginTag("tr")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("Product Name")
			writer.WriteEndTag("td")

			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("Quantity")
			writer.WriteEndTag("td")


			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("Price")
			writer.WriteEndTag("td")

			writer.WriteEndTag("tr")
			writer.WriteBeginTag("tr")

			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("Samsung Android")
			writer.WriteEndTag("td")

			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write("5")
			writer.WriteEndTag("td")

			writer.WriteBeginTag("td")
			writer.Write(HtmlTextWriter.TagRightChar)
			writer.Write(10750.51)
			writer.WriteEndTag("td")

			writer.WriteEndTag("tr")

			writer.WriteEndTag("table")



			Console.WriteLine(stringwriter.ToString())



			Console.ReadLine()

		End Sub
	End Class
End Namespace

Tags:

C# MemoryStream ExampleC# XML XmlReaderC# Recursion ExampleC# Do While Loop ExampleC# foreach loopDateTimeTryParse using C# and VB.net exampleOne dimensional array example C#.net VB.netC# Get Random Item From List (VB.net) Example.C# 4.0 Tuple ExampleC# while loopC# For Loop

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.