C#,用普通文本字符替换所有html特殊字符的函数

时间:2010-04-27 11:24:48

标签: c# html-entities

我有来自xml模板的字符,例如:

& > 

框架中是否存在泛型函数以用它们的正常等价物替换它们?

3 个答案:

答案 0 :(得分:53)

您想使用HttpUtility.HtmlDecode。:

  

将已经过HTML编码的HTTP传输字符串转换为已解码的字符串。

答案 1 :(得分:3)

也许这有帮助:WebUtility.HtmlDecode(“”);

答案 2 :(得分:0)

有时文本中的某些部分已被双重编码。

例如:"Lorem Ipsum
   - Blah"

这可能会有所帮助:

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
}