HTML with HtmlTextWriter

alysha arshad
4 min readMar 1, 2021

Hey Readers,

This is an introduction to using the HtmlTextWriter library to build a HTML Page using C# and whether you found yourself here to unblock yourself, learn something new or just by sheer accident (it happens!).

Welcome!

At the time, I was new to C# and I needed to create a simple HTML page as part of a project. I searched for something I thought would be relatively quick to pick up especially with my lack of experience with C# and came across HtmlTextWriter . Now this probably is not the best method for creating huge complex HTML pages however, I do believe for beginners this is the perfect way to achieve a HTML page within a short period of time.

For the purposes of this post, we will be building a very simple HTML page with a header and a table with images and text (see example below).

The above is rendered from this HTML code :

I will be visiting each section commented in the image above, where I will discuss the reasoning behind decisions and how to achieve it.

If you are starting from scratch:

  • I would recommend downloading Visual studio and creating a new C# project.
  • The imports needed for this project are :
using System;
using System.IO;
using System.Web.UI;

1. Declaring a HTML page

Before you can begin to code your HTML page you must initialise the StringWriter and HtmlTextWriter :

StringWriter stringWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWriter)

If you are using a script to generate multiple HTML pages, I would wrap the above in a Using statement so that the HtmlTextWriter is disposed after each use:

using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)){#create your html within here}

However, for the purposes of this blog we will go with the first instance (without the Using statement)

In order to render a HTML page successfully on browsers, a DOCTYPE tag is used to instruct the browser which mode to render with . We can create this tag using :

writer.WriteFullBeginTag("!DOCTYPE html"); // Start doc html
writer.RenderBeginTag(HtmlTextWriterTag.Html); // Begin html

2. Incorporating css styling into your HTML page

In this case, I use very simple styling for the HTML page although there are much more exciting libraries that can be used such as FlexBox.

The styles that are displayed in section 2 of the html code are extracted from a styles.css file, as shown below

This code snippet demonstrates two ways in which a tag can be rendered using HtmlTextWriter:

writer.RenderBeginTag(HtmlTextWriterTag.Head); //begin head

string styleSheetContent = File.ReadAllText("styles.css");
writer.Write($@"
<style type = 'text/css'>
{styleSheetContent}
</style>
");
writer.RenderEndTag(); // End head

1. We use writer.RenderBeginTag(HtmlTextWriterTag.Head)

  • Where we are clearly stating this is the beginning tag <>and the specific tag we are using is head. This produced <head> as shown in the HTML code.
  • This tag is then closed using writer.RenderEndTag()
  • One of the benefits of using HtmlTextWriter over hardcoded tags is that you are less likely to run into syntax errors in the HTML code

2. We can also use writer.Write(<[tag]> …</[tag]>similarly shown in the code above with the style tag

  • This allows you the ability to customise your tags and add attributes unlike the previous method

3. Adding a table with images and text

This part was slightly more confusing and I shall show you why….

*shivers*

In a perfect world, I would have three images and their base64encoded string will be generated in the code and i would then resize them however, I have replaced those magical strings with image[index]string in my array of strings. The reason for using the encoding is because whilst a URL is great locally, images won’t be “saved to memory” so when a viewer elsewhere views your page the image won’t render because they don’t have the image stored at the same URL locally. Hence why we use these huge strings which store all the info about the image on the webpage, so the images can be rendered successfully.

The first part is fairly simple, you are beginning the div, table and tbodytags as you would any other tag and we also end them at the end of everything apart from the tbody tag.

As you can see from the final three writer.RenderEndTag();, the type of tag does not need to be specified which is why comments are extremely useful to keep track.

The second part of the code initialises the header of the table and as we have 2 columns we have two table headers. We open the table header tag (<th>), add our text and then close the table header for each column.

Finally, for each image:

1. Create a row

writer.RenderBeginTag(HtmlTextWriterTag.Tr);

2. Begin the tag for the cell for the first column

writer.RenderBeginTag(HtmlTextWriterTag.Td);

3. Add text to that cell

writer.Write($”Image #{imageCount}”);

4. Close the cell for the first column

writer.RenderEndTag();

5. Begin the tag for the cell for the second column

writer.RenderBeginTag(HtmlTextWriterTag.Td);

6. Create an attribute for the image

writer.AddAttribute(HtmlTextWriterAttribute.Src, $”data:image/jpg;base64,{image[index]String}”);

7. Begin an image table add the attribute to it

writer.RenderBeginTag(HtmlTextWriterTag.Img);

Important thing to note : Attributes such as images must be declared before the tag for said attribute is begun!

8. End the image tag

writer.RenderEndTag();

9. End the table cell tag for the second column

writer.RenderEndTag();

10. End the table row

writer.RenderEndTag();

Finally, you return the html string you have just written as follows:

return stringWriter.ToString();

So we have reached the end of my introduction to HtmlTextWriter . We have covered how to get started, how to incorporate styling into the html page and finally how to create a table with images and text. I hope this post does shed more light on how to use it if you choose to.

Thank you for reading :)

References:

--

--