While working some time we need to encode or
decode our string that contains HTML.
Here is the example of encoding and decoding the html
string:
using System;
using System.Web;
using System.Diagnostics;
using System.Web.UI;
public partial
class Default
: Page
{
protected void
Page_Load(object sender, EventArgs e)
{
String
originalValue = "<h1>This is Test
Header</h1>";
String
encodedValue = HttpUtility.HtmlEncode(originalValue);
String
decodedValue = HttpUtility.HtmlDecode(encodedValue);
Debug.WriteLine(originalValue);
Debug.WriteLine(encodedValue);
Debug.WriteLine(decodedValue);
}
}
|
The output will be:
<h1>This is Test Header</h1>
<h1>This is Test Header</h1>
<h1>This is Test Header</h1>
|
If you are using .NET 4.0+ you can also use
WebUtility.HtmlEncode and WebUtility.HtmlDecode which is available in
the System.Net namespace.
Comments
Post a Comment