Sai A Sai A
Updated date Apr 13, 2024
In this blog, we will discuss four different methods to convert HTML encoded strings to normal strings in C# - HttpUtility.HtmlDecode method, Regex.Replace method, HtmlAgilityPack library, and XmlDocument class.
  • 3.1k
  • 0
  • 0

Method 1: Using HttpUtility.HtmlDecode Method

To convert HTML encoded strings to normal strings in C# by using the HttpUtility.HtmlDecode method. This method is available in the System.Web namespace and can be used to decode any HTML-encoded string.

using System;
using System.Web;

namespace HtmlDecodeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string encodedString = "This is an <example> of an HTML encoded string.";
            string decodedString = HttpUtility.HtmlDecode(encodedString);
            Console.WriteLine(decodedString);
        }
    }
}

Output:

This is an <example> of an HTML encoded string.

In this method, we first define the encoded string, which contains HTML-encoded characters. We then use the HttpUtility.HtmlDecode method to decode the string, which returns a normal string. Finally, we print the decoded string to the console.

Method 2: Using Regex.Replace Method

The second method to convert HTML encoded strings to normal strings in C# is by using the Regex.Replace method. This method is available in the System.Text.RegularExpressions namespace and can be used to replace patterns in a string.

using System;
using System.Text.RegularExpressions;

namespace RegexReplaceExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string encodedString = "This is an &lt;example&gt; of an HTML encoded string.";
            string decodedString = Regex.Replace(encodedString, @"&lt;|&gt;", m => m.Value == "&lt;" ? "<" : ">");
            Console.WriteLine(decodedString);
        }
    }
}

Output: 

This is an <example> of an HTML encoded string.

In this method, we first define the encoded string, which contains HTML encoded characters. We then use the Regex.Replace method to replace the HTML encoded characters with their normal counterparts. The regular expression @"<|>" matches both the "<" and ">" HTML entities. The lambda function m => m.Value == "<" ? "<" : ">" checks if the matched value is "<", in which case it replaces it with "<", otherwise it replaces it with ">". Finally, we print the decoded string to the console.

Method 3: Using HtmlAgilityPack

The third method to convert HTML encoded strings to normal strings in C# is by using the HtmlAgilityPack library. This library provides a set of classes for parsing and manipulating HTML documents.

using System;
using HtmlAgilityPack;

namespace HtmlAgilityPackExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string encodedString = "This is an &lt;example&gt; of an HTML encoded string.";
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(encodedString);
            string decodedString = htmlDoc.DocumentNode.InnerText;
            Console.WriteLine(decodedString);
        }
    }
}

Output:

This is an <example> of an HTML encoded string.

In this method, we first define the encoded string, which contains HTML-encoded characters. We then create a new instance of the HtmlDocument class from the HtmlAgilityPack library. We load the encoded string into the HtmlDocument object using the LoadHtml method. Finally, we use the InnerText property of the DocumentNode object to retrieve the decoded string. The InnerText property returns only the text content of the HTML document, without any HTML tags or attributes. We then print the decoded string to the console.

Method 4: Using XmlDocument Class

The fourth method to convert HTML encoded strings to normal strings in C# is by using the XmlDocument class. This class is available in the System.Xml namespace and provides a way to load, manipulate, and save XML documents.

using System;
using System.Xml;

namespace XmlDocumentExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string encodedString = "This is an &lt;example&gt; of an HTML encoded string.";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<root>" + encodedString + "</root>");
            string decodedString = xmlDoc.InnerText;
            Console.WriteLine(decodedString);
        }
    }
}

Output: 

This is an <example> of an HTML encoded string.

In this method, we first define the encoded string, which contains HTML-encoded characters. We then create a new instance of the XmlDocument class from the System.Xml namespace. We load the encoded string into the XmlDocument object using the LoadXml method. We wrap the encoded string in a root element to make it a valid XML document. Finally, we use the InnerText property of the XmlDocument object to retrieve the decoded string. We then print the decoded string to the console.

Comments (0)

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