Mohanapriya R Mohanapriya R
Updated date Jul 22, 2024
In this article, we will learn how to decode HTML-encoded strings in C# using the WebUtility class.

Decoding HTML Entities in C#:

C# provides a easy class named System.Net.WebUtility that includes the HtmlDecode method, which helps in decoding HTML-encoded strings.

using System;

class Program
{
    static void Main()
    {
        // HTML-encoded string
        string encodedString = "Hello <world>! This & is a test.";

        // Decoding the HTML-encoded string
        string decodedString = System.Net.WebUtility.HtmlDecode(encodedString);

        // Displaying the decoded string
        Console.WriteLine("Decoded String: " + decodedString);
    }
}

We hardcoded with an HTML-encoded string (encodedString) containing entities like "<" and "&". We also used the HtmlDecode method from System.Net.WebUtility to convert this encoded string into a normal string (decodedString). 

Output:

Decoded String: Hello <world>! This & is a test.

Comments (0)

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