C# 转换 & 对等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1562360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting & to & etc
提问by
I want to convert &
to &, "
to " etc.
Is there a function in c# that could do that without writing all the options manually?
我想转换&
为 &,"
为 " 等。 c# 中是否有一个函数可以在不手动编写所有选项的情况下做到这一点?
回答by Matt Hamsmith
回答by RichardOD
回答by Ian Kemp
Use the static method
使用静态方法
HttpUtility.HtmlEncode
to change &
to &
and "
to "
. Use
要改变&
到&
和"
到"
。用
HttpUtility.HtmlDecode
to do the reverse.
做相反的事情。
回答by Divina
A good article about this subject : Different ways how to escape an XML string in C#
关于这个主题的一篇好文章:如何在 C# 中转义 XML 字符串的不同方法
回答by Bartosz Wójcik
For .NET < 4 simple encoder
对于 .NET < 4 简单编码器
public static string HtmlEncode(string value)
{
return value.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
}
回答by Corredera Romain
You can use System.Net.WebUtility.HtmlDecode(uri);
您可以使用 System.Net.WebUtility.HtmlDecode(uri);
回答by Alex W.
using System.Web;
...
var html = "this is a sample & string";
var decodedhtml = HttpUtility.HtmlDecode(html);