如何转义HTML字符?在.NET中-& gt;& amp; quot;

问题描述:

如何在.NET中转义HTML字符?

How do i escape HTML characters in .NET?

我正在从json字符串中获取html,并且在标题中我得到了& quot;更多文本.看来我需要做两次才能使& amp;"成为& quot ,然后成为'"'.

I am grabbing html from a json string and in the title i get "more text. It looks like i need to do it twice to get " to become " then that to be a '"'.

如何在.NET中转义所有文本html转义代码?

How do i escape all the text html escape codes in .NET?

如果必须执行两次,则说明您对源字符串进行了双重编码,因此必须对它进行双重解码才能返回到原始(未编码))字符串.

If you have to do it twice, then you have double encoded your source string, and so you have to double decode it to get back to the original (unencoded) string.

您可以将 System.Web.HttpUtility.HtmlEncode() HtmlDecode()用于这些目的.

You can use System.Web.HttpUtility.HtmlEncode() and HtmlDecode() for these purposes.