Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode HTML escaped characters back to normal string in C#

Tags:

html

c#

escaping

My question is simple. I searched a little online, but could not find a quick way to unescape HTML text in a string.

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?

like image 689
Mario Stoilov Avatar asked Apr 11 '13 17:04

Mario Stoilov


People also ask

How do you unescape in HTML?

Unescape HTML Entities with a Text Area One way to unescape HTML entities is to put our escaped text in a text area. This will unescape the text, so we can return the unescaped text afterward by getting the text from the text area. We have an htmlDecode function that takes an input string as a parameter.

What does Httputility HtmlDecode do?

HtmlDecode(String, TextWriter)Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.

What is HtmlEncode C#?

HtmlEncode(Object)Converts an object's string representation into an HTML-encoded string, and returns the encoded string. public: static System::String ^ HtmlEncode(System::Object ^ value); C# Copy.


1 Answers

use System.Web.HttpUtility.HtmlDecode or System.Net.WebUtility.HtmlDecode

var decoded = HttpUtility.HtmlDecode("&lt; &gt; &amp;");
like image 99
I4V Avatar answered Sep 24 '22 12:09

I4V