Sai A Sai A
Updated date Apr 13, 2024
In this blog, we will explore three methods to convert strings to HTML decode in C#: using the HtmlDecode method of the HttpUtility class, the Replace method of the Regex class, and the HtmlAgilityPack library.

Method 1: Using HttpUtility.HtmlDecode Method

C# provides the HttpUtility class, which contains a static method HtmlDecode that can be used to decode HTML-encoded strings. The HtmlDecode method replaces HTML-encoded characters with their corresponding symbols. The syntax for using the HtmlDecode method is as follows:

string decodedString = HttpUtility.HtmlDecode(encodedString);

Here, encodedString is the input HTML-encoded string, and decodedString is the output string after decoding.

 

using System;
using System.Web;

class Program {
    static void Main(string[] args) {
        string encodedString = "My name is <b>John</b>";
        string decodedString = HttpUtility.HtmlDecode(encodedString);
        Console.WriteLine(decodedString);
    }
}

Output:

My name is <b>John</b>

In the above example, we have used the HtmlDecode method to decode the HTML-encoded string "My name is <b>John</b>". The output string is "My name is <b>John</b>", where the encoded character "<" is replaced by "<", and ">" is replaced by ">".

Method 2: Using Regex.Replace Method

The Regular Expression (Regex) class in C# can also be used to decode HTML-encoded strings. We can use the Replace method of the Regex class to replace the HTML-encoded characters with their corresponding symbols. The syntax for using the Regex.Replace method is as follows:

string decodedString = Regex.Replace(encodedString, @"&\w+;", m => ((char)int.Parse(m.Value.Substring(1, m.Value.Length - 2))).ToString());

Here, encodedString is the input HTML-encoded string, and decodedString is the output string after decoding.

 

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main(string[] args) {
        string encodedString = "My name is &#60;b&#62;John&#60;/b&#62;";
        string decodedString = Regex.Replace(encodedString, @"&\w+;", m => ((char)int.Parse(m.Value.Substring(1, m.Value.Length - 2))).ToString());
        Console.WriteLine(decodedString);
    }
}

Output:

My name is <b>John</b>

In the above example, we have used the Regex.Replace method to decode the HTML-encoded string "My name is <b>John</b>". The output string is "My name is <b>John</b>", where the encoded character "<" is replaced by "<", and ">" is replaced by ">".

Method 3: Using HtmlAgilityPack Library

HtmlAgilityPack is an open-source HTML parser library for .NET that can be used to parse and manipulate HTML documents. It provides a simple and flexible API to parse and manipulate HTML documents. We can use the HtmlAgilityPack library to decode HTML-encoded strings by parsing the encoded string as an HTML document and then getting the InnerText property of the parsed document. The InnerText property returns the decoded HTML content as plain text.

 

using System;
using HtmlAgilityPack;

class Program {
    static void Main(string[] args) {
        string encodedString = "My name is &#60;b&#62;John&#60;/b&#62;";
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(encodedString);
        string decodedString = htmlDoc.DocumentNode.InnerText;
        Console.WriteLine(decodedString);
    }
}

Output:

My name is <b>John</b>

In the above example, we have used the HtmlAgilityPack library to decode the HTML-encoded string "My name is <b>John</b>". First, we have created a new instance of the HtmlDocument class and loaded the encoded string as an HTML document. Then, we have used the InnerText property of the DocumentNode property of the HtmlDocument object to get the decoded HTML content as plain text.

Comments (0)

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