How can I decode HTML characters in C#?

前端 未结 10 2036
夕颜
夕颜 2020-11-22 03:38

I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?

10条回答
  •  不思量自难忘°
    2020-11-22 03:45

    For .net 4.0

    Add a reference to System.net.dll to the project with using System.Net; then use the following extensions

    // Html encode/decode
        public static string HtmDecode(this string htmlEncodedString)
        {
            if(htmlEncodedString.Length > 0)
            {
                return System.Net.WebUtility.HtmlDecode(htmlEncodedString);
            }
            else
            {
                return htmlEncodedString;
            }
        }
    
        public static string HtmEncode(this string htmlDecodedString)
        {
            if(htmlDecodedString.Length > 0)
            {
                return System.Net.WebUtility.HtmlEncode(htmlDecodedString);
            }
            else
            {
                return htmlDecodedString;
            }
        }
    

提交回复
热议问题