37

I have characters incoming from an xml template for example:

& > 

Does a generic function exist in the framework to replace these with their normal equivalents?

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
JL.
  • 78,954
  • 126
  • 311
  • 459

3 Answers3

67

You want to use HttpUtility.HtmlDecode.:

Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 2
    I have a problem with this and scandinavian characters. It does decode ex. `%20` to blank space, but `%F8` which should be an `ø` is just shown as some sort of ascii question mark figure. Any tips? – Martin at Mennt Nov 15 '11 at 00:41
7

Sometimes the text has parts that has been doubly encoded.

For example: "Lorem Ipsum
   - Blah"

This may help with that:

public static string RecursiveHtmlDecode(string str) {
    if (string.IsNullOrWhiteSpace(str)) return str;  
    var tmp = HttpUtility.HtmlDecode(str);
    while (tmp != str)
    {
        str = tmp;
        tmp = HttpUtility.HtmlDecode(str);
    }
    return str; //completely decoded string
}
jaybro
  • 1,363
  • 1
  • 12
  • 23
4

Maybe this helps: WebUtility.HtmlDecode("");

Rumata
  • 71
  • 4