C# StringWriter Use

HTML

You need to use StringWriter with HtmlTextWriter in your application written in the C# language. You can use the internal StringBuilder, which allows you to convert method return values to string types. Here we see how you can use StringWriter in your C# programs.

Using StringWriter

First, we look at a complete console program that uses StringWriter. StringWriter is implemented with an internal StringBuilder, making it have excellent performance in most scenarios involving looping. For compatibility, we can use the internal buffer of the StringWriter directly.

=== Program that uses StringWriter (C#) ===

using System;
using System.IO;
using System.Text;
using System.Web.UI;

class Program
{
    static void Main()
    {
        // Example string data
        string[] arr = new string[]
        {
            "One",
            "Two",
            "Three"
        };
        // Write markup and strings to StringWriter
        StringWriter stringWriter = new StringWriter();
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            foreach (string item in arr)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                // Send internal StringBuilder to markup method.
                WriteMarkup(item, stringWriter.GetStringBuilder());
                writer.RenderEndTag();
            }
        }
        Console.WriteLine(stringWriter.ToString());
    }

    /// <summary>
    /// Writes to StringBuilder parameter
    /// </summary>
    static void WriteMarkup(string sourceString, StringBuilder builder)
    {
        builder.Append("Some").Append(" text");
    }
}

=== Output of the program ===

<div>
Some text
</div><div>
Some text
</div><div>
Some text
</div>

It declares a new StringWriter. Here we declare a new StringWriter. This always has an internal StringBuilder. We use this for the HtmlTextWriter mainly. The HTML is written to the StringBuilder by HtmlTextWriter.

It uses GetStringBuilder(). Look at how the stringWriter.GetStringBuilder() method is used to pass a reference to the WriteMarkup method. StringBuilder is much more common than StringWriter. This can greatly improve compatibility.

It incurs no string overhead. The WriteMarkup method could return a new string containing its results, but this would be very inefficient and non-ideal. It is best to reuse buffers and write one piece at a time.

StringBuilder used with GetStringBuilder

I have written that using StringBuilder as a parameter is an excellent approach. The approach presented in this article is an extension of that. There are minimal wasted CPU cycles.

See StringBuilder Secrets.

Summary

Here we saw an example of StringWriter and its buffer. When working with a buffer or StringBuilder, always write new content to the same buffer. Don't create temporary strings and write those. We combined HtmlTextWriter, StringWriter, and StringBuilder into working code.

See StringBuilder Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen