Click here to Skip to main content
15,867,756 members
Articles / Web Development / HTML
Alternative
Tip/Trick

Remove all the HTML tags and display a plain text only inside (in case XML is not well formed)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Jan 2011CPOL 9.7K   1   2
NOTE: If you're really wanting plain text, then you should also be sure to decode the HTML entities (System.Web.HttpUtility.HtmlDecode()) on the resulting text, or you'll wind up with HTML/XML character entity text in your output, such as & and [ If you're going to immediately output the...
NOTE: If you're really wanting plain text, then you should also be sure to decode the HTML entities (System.Web.HttpUtility.HtmlDecode()) on the resulting text, or you'll wind up with HTML/XML character entity text in your output, such as & and [ If you're going to immediately output the text to a browser, however, then you won't need to.
using System.Web;
 . . .
class foo {
   public void bar() {
      string ss = "Remove tags & HTML Entities";
      Regex regex = new Regex("\\<[^\\>]*\\>");
      Response.Write(String.Format("Before: '{0}'\n", ss));
      ss = regex.Replace(ss, String.Empty);
      ss = HttpUtility.HtmlDecode(ss);
      Response.Write(String.Format("After: '{0}'\n", ss));
   }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Good Expansion of trick. Pin
Hiren solanki27-Dec-10 20:25
Hiren solanki27-Dec-10 20:25 
GeneralYou found a loophole and that's fantastic to decode that cha... Pin
Hiren solanki27-Dec-10 20:24
Hiren solanki27-Dec-10 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.