How do i escape HTML characters in .NET?
如何在.NET中转义HTML字符?
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 '"'.
我从json字符串中抓取html,在标题中我得到& quot;更多文本。看起来我需要做两次以获得& quot;成为“那就是'''。
How do i escape all the text html escape codes in .NET?
如何在.NET中转义所有文本html转义码?
4 个解决方案
#1
26
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.
如果你必须做两次,那么你已经对你的源字符串进行了双重编码,因此你必须对其进行双重解码才能返回到原始(未编码的)字符串。
You can use System.Web.HttpUtility.HtmlEncode()
and HtmlDecode()
for these purposes.
您可以使用System.Web.HttpUtility.HtmlEncode()和HtmlDecode()来实现这些目的。
#2
4
Have a look at the HttpUtility class. It has some helpful static functions.
看看HttpUtility类。它有一些有用的静态函数。
However, as @codekaizen points out, the example you have looks double encoded. If you don't have control of the source, then play with the HtmlDecode
and (maybe?) UrlDecode
functions in HttpUtility so that maybe you luckily undo the source's mistakes.
但是,正如@codekaizen指出的那样,你看到的例子看起来是双重编码的。如果你没有控制源,那么在HttpUtility中使用HtmlDecode和(可能是?)UrlDecode函数,这样你就可以幸运地撤消源代码的错误。
#3
2
It looks like your original string was double-encoded. quot;
is only a partial entity, and it can't be decoded into the character by itself. If you are getting the title from a web page you don't control, there is little you can do except, as you not, double-decode.
看起来您的原始字符串是双重编码的。 QUOT;只是一个部分实体,它不能自己解码成字符。如果你从一个你无法控制的网页上获得标题,那么你几乎无法做到,除非你没有,否则需要进行双重解码。
#4
1
Yes, you would have to HTML-decode the string twice, as it seems to be encoded twice.
是的,你必须对字符串进行两次HTML解码,因为它似乎被编码了两次。
You should perhaps look at the source instead, and find out why the string is HTML-encoded in the first place. It shouldn't be, not even once. A JSON string is not HTML so it shouldn't be HTML-encoded at all.
您可能应该查看源代码,并找出字符串首先是HTML编码的原因。它不应该,甚至不是一次。 JSON字符串不是HTML,因此它根本不应该是HTML编码的。
To encode a quotation mark in a JSON string you use backslash, so correct JSON should look something like:
要在JSON字符串中编码引号,请使用反斜杠,因此正确的JSON应如下所示:
{"title":"\"I Won't Let Them Take You\""}
#1
26
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.
如果你必须做两次,那么你已经对你的源字符串进行了双重编码,因此你必须对其进行双重解码才能返回到原始(未编码的)字符串。
You can use System.Web.HttpUtility.HtmlEncode()
and HtmlDecode()
for these purposes.
您可以使用System.Web.HttpUtility.HtmlEncode()和HtmlDecode()来实现这些目的。
#2
4
Have a look at the HttpUtility class. It has some helpful static functions.
看看HttpUtility类。它有一些有用的静态函数。
However, as @codekaizen points out, the example you have looks double encoded. If you don't have control of the source, then play with the HtmlDecode
and (maybe?) UrlDecode
functions in HttpUtility so that maybe you luckily undo the source's mistakes.
但是,正如@codekaizen指出的那样,你看到的例子看起来是双重编码的。如果你没有控制源,那么在HttpUtility中使用HtmlDecode和(可能是?)UrlDecode函数,这样你就可以幸运地撤消源代码的错误。
#3
2
It looks like your original string was double-encoded. quot;
is only a partial entity, and it can't be decoded into the character by itself. If you are getting the title from a web page you don't control, there is little you can do except, as you not, double-decode.
看起来您的原始字符串是双重编码的。 QUOT;只是一个部分实体,它不能自己解码成字符。如果你从一个你无法控制的网页上获得标题,那么你几乎无法做到,除非你没有,否则需要进行双重解码。
#4
1
Yes, you would have to HTML-decode the string twice, as it seems to be encoded twice.
是的,你必须对字符串进行两次HTML解码,因为它似乎被编码了两次。
You should perhaps look at the source instead, and find out why the string is HTML-encoded in the first place. It shouldn't be, not even once. A JSON string is not HTML so it shouldn't be HTML-encoded at all.
您可能应该查看源代码,并找出字符串首先是HTML编码的原因。它不应该,甚至不是一次。 JSON字符串不是HTML,因此它根本不应该是HTML编码的。
To encode a quotation mark in a JSON string you use backslash, so correct JSON should look something like:
要在JSON字符串中编码引号,请使用反斜杠,因此正确的JSON应如下所示:
{"title":"\"I Won't Let Them Take You\""}