How can I decode HTML characters in C# ?



  • 9 Answer(s)

    The best approach is using HttpUtility.HtmlDecode method.

        For .NET 4.0+, use WebUtility.HtmlDecode in which there is no extra assembly reference as it is obtained in the System.Net namespace method.

    Answered on December 28, 2018.
    Add Comment

    Using 4.0 without including assembly:

    System.Net.WebUtility.HtmlDecode()
    
    Answered on December 28, 2018.
    Add Comment

    Add a reference to System.Web.dll for a non-ASP .NET application.Follow the steps:

    • Right-click the project in Solution Explorer.
    • Select “Add Reference”
    • Browse the list for System.Web.dll.

    From this the reference is added to access the method using the fully-qualified name System.Web.HttpUtility.HtmlDecode or  embed System.Web by using statement.

    Answered on December 28, 2018.
    Add Comment

    To decode HTML take a look below code

    string s = "Svendborg Værft A/S";
    string a = HttpUtility.HtmlDecode(s).ToString();
    Response.Write(a);

    Output is like

     Svendborg Værft A/S
    Answered on January 15, 2019.
    Add Comment

    The following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. The input string is encoded using the HtmlEncode method. The encoded string obtained is then decoded using the HtmlDecode method.

    C#
    using System;
    using System.Web;
    using System.IO;
    
    class MyNewClass
    {
        public static void Main()
        {
            Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
            string myString = Console.ReadLine();
    
            // Encode the string.
            string myEncodedString = HttpUtility.HtmlEncode(myString);
    
            Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
            StringWriter myWriter = new StringWriter();
    
            // Decode the encoded string.
            HttpUtility.HtmlDecode(myEncodedString, myWriter);
    
            string myDecodedString = myWriter.ToString();
            Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
        }
    }
    Answered on January 15, 2019.
    Add Comment

    The following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. The input string is encoded using the HtmlEncode method. The encoded string obtained is then decoded using the HtmlDecode method.

    C#
    using System;
    using System.Web;
    using System.IO;
    
    class MyNewClass
    {
        public static void Main()
        {
            Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
            string myString = Console.ReadLine();
    
            // Encode the string.
            string myEncodedString = HttpUtility.HtmlEncode(myString);
    
            Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
            StringWriter myWriter = new StringWriter();
    
            // Decode the encoded string.
            HttpUtility.HtmlDecode(myEncodedString, myWriter);
    
            string myDecodedString = myWriter.ToString();
            Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
        }
    }
    Answered on January 15, 2019.
    Add Comment

    String as parameter and return the decoded html string.

    Include the using System.Web.HttpUtility into your class

    public static string HtmlEncode(string text)
        {
            if(text.length > 0){
    
               return HttpUtility.HtmlDecode(text);
            }else{
    
             return text;
            }
    
        }
    Answered on January 16, 2019.
    Add Comment
    1. string DecodeHtmlChars(string aText)
    2. {
    3. string[] parts = aText.Split(new string[]{"&#x"}, StringSplitOptions.None);
    4. for (int i = 1; i < parts.Length; i++)
    5. {
    6. int n = parts[i].IndexOf(';');
    7. string number = parts[i].Substring(0,n);
    8. try
    9. {
    10. int unicode = Convert.ToInt32(number,16)
    11. parts[i] = ((char)unicode) + parts[i].Substring(n+1);
    12. } catch {}
    13. }
    14. return String.Join("",parts);
    15. }
    Answered on January 16, 2019.
    Add Comment

    It is also worth mentioning that if you’re using Html Agility Pack like I was, you should use HtmlAgilityPack.HtmlEntity.DeEntitize(). It takes a string and returns a string.

    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.