转换&到&等等

时间:2009-10-13 19:19:30

标签: c# html-encode string-conversion

我想将&转换为&,"为“等。 如果没有手动编写所有选项,c#中是否有一个函数可以做到这一点?

7 个答案:

答案 0 :(得分:92)

System.Web.HttpUtility.HtmlDecode()

修改:请注意here“要对网络应用程序之外的值进行编码或解码,请使用...”

System.Net.WebUtility.HtmlDecode()

答案 1 :(得分:23)

使用静态方法

HttpUtility.HtmlEncode

&更改为&,将"更改为"。使用

HttpUtility.HtmlDecode

反过来。

答案 2 :(得分:17)

您可以使用System.Net.WebUtility.HtmlDecode(uri);

答案 3 :(得分:4)

答案 4 :(得分:4)

关于此主题的好文章:Different ways how to escape an XML string in C#

答案 5 :(得分:2)

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

答案 6 :(得分:-3)

对于.NET< 4个简单的编码器

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