How Can I strip HTML from Text in .NET?

.net, asp.net, c#, html, jquery

Solution

I downloaded the HtmlAgilityPack and created this function:

string StripHtml(string html)
{
    // create whitespace between html elements, so that words do not run together
    html = html.Replace(">","> ");

    // parse html
    var doc = new HtmlAgilityPack.HtmlDocument();   
    doc.LoadHtml(html);

    // strip html decoded text from html
    string text = HttpUtility.HtmlDecode(doc.DocumentNode.InnerText);   

    // replace all whitespace with a single space and remove leading and trailing whitespace
    return Regex.Replace(text, @"\s+", " ").Trim();
}

Problem

I have an asp.net web page that has a TinyMCE box. Users can format text and send the HTML to be stored in a database. On the server, I would like to take strip the html from the text so I can store only the text in a Full Text indexed column for searching. It's a breeze to strip the html on the client using jQuery's text() function, but I would really rather do this on the server. Are there any existing utilities that I can use for this? EDIT See my answer. EDIT 2 alt text http://tinyurl.com/sillychimp

Original source