简体   繁体   English

在C#中将HTML转义字符解码回普通字符串

[英]Decode HTML escaped characters back to normal string in C#

My question is simple. 我的问题很简单。 I searched a little online, but could not find a quick way to unescape HTML text in a string. 我在网上搜索了一下,但找不到快速浏览字符串中HTML文本的方法。

For example: 例如:
"&lt; &gt; &amp;" should be returned to "< > &" as a string. 应该作为字符串返回“<>&”。

Is there a quick way, or do I have to write my own unescaper? 有快速的方式,还是我必须写自己的unescaper?

使用System.Web.HttpUtility.HtmlDecodeSystem.Net.WebUtility.HtmlDecode

var decoded = HttpUtility.HtmlDecode("&lt; &gt; &amp;");

如果您使用的是.NET 4.5,则可以使用HttpUtility.HtmlDecode方法。

HttpUtility.UrlDecode("Your escaped String", System.Text.Encoding.Default);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM