Converting & to & etc

前端 未结 6 625
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 11:54

I want to convert & to &, " to \" etc. Is there a function in c# that could do that without writing all the options manually?<

相关标签:
6条回答
  • 2020-12-28 11:56

    Use the static method

    HttpUtility.HtmlEncode
    

    to change & to &amp; and " to &quot;. Use

    HttpUtility.HtmlDecode
    

    to do the reverse.

    0 讨论(0)
  • 2020-12-28 11:59
    System.Web.HttpUtility.HtmlDecode()
    

    Edit: Note from here that "To encode or decode values outside of a web application, use..."

    System.Net.WebUtility.HtmlDecode()
    
    0 讨论(0)
  • 2020-12-28 12:02
    using System.Web; 
    ...
    var html = "this is a sample &amp; string"; 
    var decodedhtml = HttpUtility.HtmlDecode(html);
    
    0 讨论(0)
  • 2020-12-28 12:10

    You can use System.Net.WebUtility.HtmlDecode(uri);

    0 讨论(0)
  • 2020-12-28 12:11

    For .NET < 4 simple encoder

        public static string HtmlEncode(string value)
        {
            return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
        }
    
    0 讨论(0)
  • 2020-12-28 12:21

    Server.HtmlDecode.

    0 讨论(0)
提交回复
热议问题