Steven Spielberg
Steven Spielberg

Reputation:

How to remove html tags from string in c#

I have something like this

<h1> I am Text </h1>

in asp.net MVC

If I want to write it by <%: mystring%> that he show the tags who filled by someone.

How I can decode it then show " I am Text "?

I just want to show the source from CKEDITOR to page without HTML tags. If I use regex then the all tags hide even user use to fill the information.

Upvotes: 1

Views: 4098

Answers (4)

Naveed
Naveed

Reputation: 42093

String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

More:

Upvotes: 3

VinayC
VinayC

Reputation: 49165

Use library such as HTML Agility Pack. You may use Regular Expressions but I wouldn't recommend it.

EDIT: Here's the sample code that does html to text conversion - http://htmlagilitypack.codeplex.com/SourceControl/changeset/view/66017#1336937

Upvotes: 4

Shekhar
Shekhar

Reputation: 11788

You can use Regex to remove those html tags. I hope you are aware of Regex class in C#.

Use this regex : <[^>]*?>

Replase all the matches with blank string ""

Upvotes: 0

zerkms
zerkms

Reputation: 254886

Yes, I will be burned in hell for using regexes to work with html

var result = Regex.Replace(inputString, '<[^>]*>', string.Empty);

but this solution looks much simple than building FSM.

Upvotes: 0

Related Questions