451

I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?

10 Answers 10

686

You can use HttpUtility.HtmlDecode.

If you are using .NET 4.0+ you can also use WebUtility.HtmlDecode which does not require an extra assembly reference as it is available in the System.Net namespace.

Sign up to request clarification or add additional context in comments.

3 Comments

Add a reference to System.Web.Dll in your project properties. The classes you see live in System.dll which is referenced by default.
In case you're trying trying to decode the Query String, you need to use HttpUtility.UrlDecode
does not decode é none of the answers do. HtmlUtility/WebUtility cannot do this.
208

On .Net 4.0:

System.Net.WebUtility.HtmlDecode()

No need to include assembly for a C# project

4 Comments

It is better solution because HttpUtility doesn't decode "'" symbol.. I don't know why..
This is required in developing for the Universal Windows platform.
Will this cause XSS in .Net web pages?
Thanks. Exactly what I was looking for
46

As @CQ says, you need to use HttpUtility.HtmlDecode, but it's not available in a non-ASP .NET project by default.

For a non-ASP .NET application, you need to add a reference to System.Web.dll. Right-click your project in Solution Explorer, select "Add Reference", then browse the list for System.Web.dll.

Now that the reference is added, you should be able to access the method using the fully-qualified name System.Web.HttpUtility.HtmlDecode or insert a using statement for System.Web to make things easier.

Comments

17

To decode HTML take a look below code

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

Output is like

 Svendborg Værft A/S

1 Comment

The 'ToString()' is redundant since HtmlDecode returns a string
16

If there is no Server context (i.e your running offline), you can use HttpUtility.HtmlDecode.

1 Comment

Agreed, that's why I use HttpUtility, fell into same trap =P
13

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

Comments

7

Use Server.HtmlDecode to decode the HTML entities. If you want to escape the HTML, i.e. display the < and > character to the user, use Server.HtmlEncode.

1 Comment

There may not be a server context (i.e. when running test cases and the like) I fell in to this trap before :)
1

Write static a method into some utility class, which accept 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;
        }

    }

Comments

1

For .net 4.0

Add a reference to System.net.dll to the project with using System.Net; then use the following extensions

// Html encode/decode
    public static string HtmDecode(this string htmlEncodedString)
    {
        if(htmlEncodedString.Length > 0)
        {
            return System.Net.WebUtility.HtmlDecode(htmlEncodedString);
        }
        else
        {
            return htmlEncodedString;
        }
    }

    public static string HtmEncode(this string htmlDecodedString)
    {
        if(htmlDecodedString.Length > 0)
        {
            return System.Net.WebUtility.HtmlEncode(htmlDecodedString);
        }
        else
        {
            return htmlDecodedString;
        }
    }

Comments

1

For strings containing &#x20; I've had to double-decode the string. First decode would turn it into the second pass would correctly decode it to the expected character.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.