Convert HTML To Text
// Copyright (c) 2010
// by http://openlightgroup.net/
using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Net.Mail;
using System.Web.Mail;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Web.Security;
namespace SilverlightDebateForum
{
public class Utility
{
#region ConvertToText
public static string ConvertToText(string sHTML)
{
string sContent = sHTML;
sContent = sContent.Replace("<br />", Environment.NewLine);
sContent = sContent.Replace("<br>", Environment.NewLine);
sContent = FormatText(sContent, true);
return StripTags(sContent, true);
}
#endregion
#region FormatText
public static string FormatText(string HTML, bool RetainSpace)
{
//Match all variants of <br> tag (<br>, <BR>, <br/>, including embedded space
string brMatch = "\\s*<\\s*[bB][rR]\\s*/\\s*>\\s*";
//Replace Tags by replacement String and return mofified string
return System.Text.RegularExpressions.Regex.Replace(HTML, brMatch, Environment.NewLine);
}
#endregion
#region StripTags
public static string StripTags(string HTML, bool RetainSpace)
{
//Set up Replacement String
string RepString;
if (RetainSpace)
{
RepString = " ";
}
else
{
RepString = "";
}
//Replace Tags by replacement String and return mofified string
return System.Text.RegularExpressions.Regex.Replace(HTML, "<[^>]*>", RepString);
}
#endregion
}
}
Related examples in the same category