从HTML字符串中获取纯文本字符串的最佳方法是什么?
public string GetPlainText(string htmlString)
{
// any .NET built in utility?
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
Ale*_* K. 37
您可以使用MSHTML,这可能非常宽容;
//using microsoft.mshtml
HTMLDocument htmldoc = new HTMLDocument();
IHTMLDocument2 htmldoc2 = (IHTMLDocument2)htmldoc;
htmldoc2.write(new object[] { "<p>Plateau <i>of<i> <b>Leng</b><hr /><b erp=\"arp\">2 sugars please</b> <xxx>what? & who?" });
string txt = htmldoc2.body.outerText;
Run Code Online (Sandbox Code Playgroud)
冷2糖的高原请问什么?& 谁?
Rud*_*ser 23
据我所知,没有内置实用程序,但根据您的要求,您可以使用正则表达式去除所有标记:
string htmlString = @"<p>I'm HTML!</p>";
Regex.Replace(htmlString, @"<(.|\n)*?>", "");
Run Code Online (Sandbox Code Playgroud)