Sai A Sai A
Updated date Aug 04, 2023
In this blog, we will learn how to convert plain text to HTML in C#. Explore three different methods – String Concatenation, Regular Expressions, and the HTML Agility Pack – each accompanied by code examples and outputs.

Introduction:

As a web developer, one of the common tasks is to convert plain text into HTML for display on web pages. This blog will explore the different approaches to achieve this in C#. We'll discuss three methods: String Concatenation, Regular Expressions, and the HTML Agility Pack.

Why Convert Plain Text to HTML?

HTML is the language of the web, and it allows us to structure and present content on web pages. When dealing with dynamic content, like user-generated input or data fetched from databases, plain text must be formatted as HTML to ensure proper presentation. Converting plain text to HTML enables us to apply styling, insert hyperlinks, and include multimedia elements, creating a more engaging user experience.

Method 1: Using String Concatenation

String concatenation is a straightforward approach to converting plain text to HTML. We'll manually build HTML tags and format the text accordingly. Here's a program that demonstrates this method:

using System;

public class PlainTextToHtmlConverter
{
    public static string ConvertToHtmlUsingConcatenation(string plainText)
    {
        // Replace newlines with <br> tags
        string html = plainText.Replace(Environment.NewLine, "<br>");

        // Wrap the whole text in <p> tags
        html = $"<p>{html}</p>";

        return html;
    }
}

class Program
{
    static void Main()
    {
        string plainText = "Hello, this is a plain text.\nIt will be converted to HTML.";
        string html = PlainTextToHtmlConverter.ConvertToHtmlUsingConcatenation(plainText);
        Console.WriteLine(html);
    }
}

Output:

<p>Hello, this is a plain text.<br>It will be converted to HTML.</p>

Method 2: Using Regular Expressions

Regular expressions provide a powerful way to manipulate text patterns. This method allows us to identify specific elements in plain text and replace them with corresponding HTML tags. Here's the program:

using System;
using System.Text.RegularExpressions;

public class PlainTextToHtmlConverter
{
    public static string ConvertToHtmlUsingRegex(string plainText)
    {
        // Replace newlines with <br> tags using regular expressions
        string html = Regex.Replace(plainText, "\r\n|\n\r|\n|\r", "<br>");

        // Wrap the whole text in <p> tags
        html = $"<p>{html}</p>";

        return html;
    }
}

class Program
{
    static void Main()
    {
        string plainText = "Hello, this is a plain text.\nIt will be converted to HTML.";
        string html = PlainTextToHtmlConverter.ConvertToHtmlUsingRegex(plainText);
        Console.WriteLine(html);
    }
}

Output:

<p>Hello, this is a plain text.<br>It will be converted to HTML.</p>

Method 3: Using HTML Agility Pack

The HTML Agility Pack is a powerful library for parsing and manipulating HTML documents. It provides a cleaner and more maintainable solution for complex HTML conversions. Let's see how to use it:

using System;
using HtmlAgilityPack;

public class PlainTextToHtmlConverter
{
    public static string ConvertToHtmlUsingHtmlAgilityPack(string plainText)
    {
        // Create an HTML document
        var doc = new HtmlDocument();

        // Create a new paragraph element
        var p = doc.CreateElement("p");

        // Set the inner text as the plain text
        p.InnerHtml = plainText;

        // Append the paragraph to the document
        doc.DocumentNode.AppendChild(p);

        return doc.DocumentNode.OuterHtml;
    }
}

class Program
{
    static void Main()
    {
        string plainText = "Hello, this is a plain text.\nIt will be converted to HTML.";
        string html = PlainTextToHtmlConverter.ConvertToHtmlUsingHtmlAgilityPack(plainText);
        Console.WriteLine(html);
    }
}

Output:

<p>Hello, this is a plain text.<br>It will be converted to HTML.</p>

Conclusion:

In this blog, we explored three different methods to convert plain text to HTML in C#: String Concatenation, Regular Expressions, and the HTML Agility Pack. The string concatenation method is simple but may become cumbersome for larger conversions. Regular expressions offer more control but require an understanding of patterns. The HTML Agility Pack is a powerful library that provides a cleaner solution for complex conversions.

Comments (0)

There are no comments. Be the first to comment!!!