如何在 C# 中解码 HTML 字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/122641/
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
How can I decode HTML characters in C#?
提问by Vasil
I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?
我有用 HTML 字符实体编码的电子邮件地址。.NET 中是否有任何东西可以将它们转换为纯字符串?
采纳答案by Quintin Robinson
You can use HttpUtility.HtmlDecode
您可以使用 HttpUtility.HtmlDecode
If you are using .NET 4.0+ you can also use WebUtility.HtmlDecode
which does not require an extra assembly reference as it is available in the System.Net
namespace.
如果您使用 .NET 4.0+,您还可以使用WebUtility.HtmlDecode
which 不需要额外的程序集引用,因为它在System.Net
命名空间中可用。
回答by Daniel Schierbeck
Use Server.HtmlDecode
to decode the HTML entities. If you want to escapethe HTML, i.e. display the <
and >
character to the user, use Server.HtmlEncode
.
使用Server.HtmlDecode
的HTML实体解码。如果要转义HTML,即向用户显示<
和>
字符,请使用Server.HtmlEncode
.
回答by Rob Cooper
If there is no Server context (i.e your running offline), you can use HttpUtility.HtmlDecode.
如果没有服务器上下文(即您离线运行),您可以使用HttpUtility。Html解码。
回答by OwenP
As @CQ says, you need to use HttpUtility.HtmlDecode, but it's not available in a non-ASP .NET project by default.
正如@CQ 所说,您需要使用HttpUtility.HtmlDecode,但默认情况下它在非 ASP .NET 项目中不可用。
For a non-ASP .NET application, you need to add a reference to System.Web.dll
. Right-click your project in Solution Explorer, select "Add Reference", then browse the list for System.Web.dll
.
对于非 ASP .NET 应用程序,您需要添加对System.Web.dll
. 在解决方案资源管理器中右键单击您的项目,选择“添加引用”,然后浏览System.Web.dll
.
Now that the reference is added, you should be able to access the method using the fully-qualified name System.Web.HttpUtility.HtmlDecode
or insert a using
statement for System.Web
to make things easier.
现在添加了引用,您应该能够使用完全限定名称访问该方法System.Web.HttpUtility.HtmlDecode
或插入一条using
语句System.Web
以简化操作。
回答by Indy9000
On .Net 4.0:
在 .Net 4.0 上:
System.Net.WebUtility.HtmlDecode()
No need to include assembly for a C# project
无需为 C# 项目包含程序集
回答by Hypershadsy
It is also worth mentioning that if you're using HtmlAgilityPack like I was, you should use HtmlAgilityPack.HtmlEntity.DeEntitize()
. It takes a string
and returns a string
.
还值得一提的是,如果您像我一样使用 HtmlAgilityPack,则应该使用HtmlAgilityPack.HtmlEntity.DeEntitize()
. 它需要 astring
并返回 a string
。
回答by Abhishek Jaiswal
To decode HTML take a look below code
要解码 HTML,请查看下面的代码
string s = "Svendborg Værft A/S";
string a = HttpUtility.HtmlDecode(s);
Response.Write(a);
Output is like
输出就像
Svendborg V?rft A/S
回答by Tahir Alvi
Write static a method into some utility class, which accept string as parameter and return the decoded html string.
将静态方法写入某个实用程序类,该类接受字符串作为参数并返回解码后的 html 字符串。
Include the using System.Web.HttpUtility
into your class
包括using System.Web.HttpUtility
到你的班级
public static string HtmlEncode(string text)
{
if(text.length > 0){
return HttpUtility.HtmlDecode(text);
}else{
return text;
}
}
回答by Vinod Srivastav
For .net 4.0
对于 .net 4.0
Add a reference to System.net.dll
to the project with using System.Net;
then use the following extensions
添加System.net.dll
对项目的引用,using System.Net;
然后使用以下扩展
// 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;
}
}
回答by self.name
For strings containing   I've had to double-decode the string. First decode would turn it into the second pass would correctly decode it to the expected character.
对于包含 的字符串 我不得不对字符串进行双重解码。第一次解码会将其转换为第二次通过将其正确解码为预期的字符。