C#, function to replace all html special characters with normal text characters

后端 未结 3 718
萌比男神i
萌比男神i 2020-12-09 08:24

I have characters incoming from an xml template for example:

& > 

Does a generic function exist in the framework to replace

相关标签:
3条回答
  • 2020-12-09 08:30

    You want to use HttpUtility.HtmlDecode.:

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

    0 讨论(0)
  • 2020-12-09 08:35

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

    0 讨论(0)
  • 2020-12-09 08:38

    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
    }
    
    0 讨论(0)
提交回复
热议问题