Search 
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 2 Comments
Rate it    (Rated 3 by 2 people)
926 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

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 .

Daily Coder 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)*?>", "");

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved