Я загрузил HtmlAgilityPack и создал эту функцию:
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();
}
Взгляните на это Удалите HTML-теги из строки с помощью регулярных выражений
Here's Jeff Atwood's RefactorMe code link for his Sanitize HTML method
If you are just storing text for indexing then you probably want to do a bit more than just remove the HTML, such as ignoring stop-words and removing words shorter than (say) 3 characters. However, a simple tag and stripper I once wrote goes something like this:
public static string StripTags(string value)
{
if (value == null)
return string.Empty;
string pattern = @"&.{1,8};";
value = Regex.Replace(value, pattern, " ");
pattern = @"<(.|\n)*?>";
return Regex.Replace(value, pattern, string.Empty);
}
It's old and I'm sure it can be optimised (perhaps using a compiled reg-ex?). But it does work and may help...
You could:
Вы можете использовать что-то вроде этого
string strwithouthtmltag;
strwithouthtmltag = Regex.Replace(strWithHTMLTags, "<[^>]*>", string.Empty)
Поскольку вы могли неправильно отформатировать HTML в системе: Можно использовать BeautifulSoup или аналогичный.
Написано на Python; Я не уверен, как это можно связать - используя язык .NET IronPython?
Вы можете использовать HTQL COM, и запросить источник запросом:
&tx;