0

我需要在特定位置去除 Word HTML 标签。目前我正在这样做:

public string CleanWordStyle(string html)
{
    StringCollection sc = new StringCollection();
    sc.Add(@"<table\b[^>]*>(.*?)</table>");
    sc.Add(@"(<o:|</o:)[^>]+>");
    sc.Add(@"(<v:|</v:)[^>]+>");
    sc.Add(@"(<st1:|</st1:)[^>]+>");
    sc.Add(@"(mso-bidi-|mso-fareast|mso-spacerun:|mso-list: ign|mso-ascii|mso-hansi|mso-ansi|mso-element|mso-special|mso-highlight|mso-border|mso-yfti|mso-padding|mso-background|mso-tab|mso-width|mso-height|mso-pagination|mso-theme|mso-outline)[^;]+;");
    sc.Add(@"(font-size|font-family):[^;]+;");
    sc.Add(@"font:[^;]+;");
    sc.Add(@"line-height:[^;]+;");
    sc.Add(@"class=""mso[^""]+""");
    sc.Add(@"times new roman&quot;,&quot;serif&quot;;");
    sc.Add(@"verdana&quot;,&quot;sans-serif&quot;;");
    sc.Add(@"<p> </p>");
    sc.Add(@"<p>&nbsp;</p>");
    foreach (string s in sc)
    {
        html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase);
    }
    html = Regex.Replace(html, @"&nbsp;", @"&#160;"); //can not be read by as XmlDocument if not!
    return html;
}

现在我正在剥离带有 的<p>标签的整个 HTML sc.Add(@"<p> </p>");,但我想要的是:如果我点击表格标签,它应该停止替换,直到它点击表格结束标签。可能吗?

4

1 回答 1

0

正则表达式可以用于一行或非常简单的 html 结构。

如果您真的想用最少的代码完成工作,请从http://htmlagilitypack.codeplex.com/获取 HTMLAgilityPack,并从所有标签的内部值中获取所有文本。

这将很简单:

public string CleanWordStyle(string htmlPage)
{
  HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  doc.LoadHtml(htmlPage);

  return doc.DocumentNode.InnerText;
}
于 2012-07-06T08:35:55.503 回答