39

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?

3 Answers 3

69

You want to use HttpUtility.HtmlDecode.:

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

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

1 Comment

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?
10

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
}

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.