DailyCoding > General

How to Html Encode a string in .NET

How to html encode a string in .net for displaying on a page
Author admin on May 18, 2008 5 Comments
Rate it    (Rated 3 by 6 people)
17,485 Views

Html Encoding is basically converting Html tags in non Html tags so that they can be embeded into a html code. Below code explains how rendering of a html string may vary because of html encoding

string htmlString = "<b>Hello world!</b>";

Response.Write(htmlString);
// This will print: Hello world!

string htmlEncoded = Server.HtmlEncode(htmlString);
Response.Write(htmlEncoded);
// This will print: <b>Hello world!</b>

You may want to Html encode a string before showing any user input on the page

C# | Utility | Web
 

Discussion

Ashish nigam On Aug 1, 2008 02:11 AM
How to remove all the tag from a web page and fetch only real data from page .

Ramesh Soni On Aug 1, 2008 02:33 AM
Here is quick trick using regular expression:

string htmlText = "input html text here";
string plaintext = Regex.Replace(htmlText, @"<(.|\n)*?>", "");

mksamy On Nov 13, 2008 04:17 AM

Dav On Jan 17, 2010 05:35 AM
You also can use an <a href="http://www.stringfun....html">online html encoder</a>
David

Dav On Jan 17, 2010 05:37 AM
Sorry to post twice, but my previous post messed up
You also can use an online html encoder -> http://www.stringfunction.com/html-encode.html
David

Leave a Comment

Name
Email Address
Web Site