views:

834

answers:

2

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?

+9  A: 

You want to use HttpUtility.HtmlDecode.:

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

Andrew Hare
A: 

The above answer is fantastic, though it is in System.Web, so I thought I'd just give something that would suffice for a forms or console app, though if there is anything better than just writing it out explicitly, I am just as curious.

private string ReplaceSpecialCharacters(string Input)
{
        Input = Regex.Replace(Input, "&", "&");
        Input = Regex.Replace(Input, "'", "'");
        Input = Regex.Replace(Input, """, "\"");
        Input = Regex.Replace(Input, "&#x2039;", "<");
        Input = Regex.Replace(Input, "&#x203A;", ">");
        Input = Regex.Replace(Input, "&gt;", ">=");
        Input = Regex.Replace(Input, "&lt;", "<=");

        return Input;
}
Brandi