Converting & to & etc

北慕城南 提交于 2019-11-28 23:32:17

问题


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


回答1:


System.Web.HttpUtility.HtmlDecode()

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

System.Net.WebUtility.HtmlDecode()



回答2:


Use the static method

HttpUtility.HtmlEncode

to change & to & and " to ". Use

HttpUtility.HtmlDecode

to do the reverse.




回答3:


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




回答4:


Server.HtmlDecode.




回答5:


A good article about this subject : Different ways how to escape an XML string in C#




回答6:


using System.Web; 
...
var html = "this is a sample & string"; 
var decodedhtml = HttpUtility.HtmlDecode(html);



回答7:


For .NET < 4 simple encoder

    public static string HtmlEncode(string value)
    {
        return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
    }


来源:https://stackoverflow.com/questions/1562360/converting-amp-to-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!