Reputation:
How do i escape HTML characters in .NET?
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 '"'.
How do i escape all the text html escape codes in .NET?
Upvotes: 17
Views: 13882
Reputation: 700162
Yes, you would have to HTML-decode the string twice, as it seems to be encoded twice.
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.
To encode a quotation mark in a JSON string you use backslash, so correct JSON should look something like:
{"title":"\"I Won't Let Them Take You\""}
Upvotes: 1
Reputation: 15579
Have a look at the HttpUtility class. It has some helpful static functions.
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.
Upvotes: 4
Reputation: 10415
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.
Upvotes: 35
Reputation: 27419
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.
Upvotes: 2