1

I am currently pulling some data from a SQL Server database into a C# asp.net page.

I am using the following (portion of) code to ouput the HTML text that is stored in my body column:

TextLabel.Text += "<div class=\"newsEntry\">" + 
    Convert.ToString(reader2["body"]).Substring(0, 220) + 
    "...<a href='entry.aspx?ID=" + reader2["ID"] + 
    "' title=\"Read More about " + reader2["Title"] + 
    "\">Read More &raquo;</a></div>";

I'd like to strip the body value (+ Convert.ToString(reader2["body"]).Substring(0, 220) +) of all HTML inside it.

How do I achieve this with C#?

Normally with PHP, I'd use striptags.

Many thanks for any help.

4
  • 2
    strip_tags and htmlspecialchars do totally different things. Make up your mind. Commented Feb 12, 2013 at 10:16
  • I've just made it up. I'd like to strip all the HTML inside body :) Any ideas? Commented Feb 12, 2013 at 10:17
  • 1
    Whatever you're trying to do with parsing HTML in C#, look first to the HTML Agility Pack - htmlagilitypack.codeplex.com Commented Feb 12, 2013 at 10:34
  • 1
    If you are always using "<div class=\"newsEntry\">" this you can use simply split function. Otherwise if your body has html tags you can use agilitypack. Commented Feb 12, 2013 at 11:57

1 Answer 1

3

look at HttpServerUtility.HtmlEncode() and HttpServerUtility.HtmlDecode() methods

Sign up to request clarification or add additional context in comments.

2 Comments

it will code and decode html characters like "<" to "&lt;" what is the relation of it in striping.
Aaaah great. So on entry ENCODE and on output DECODE? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.