2012-07-06 65 views
0

我需要去除特定位置的Word HTML标记。目前,我这样做:现在我剥整个HTML为<p>标签与sc.Add(@"<p> </p>");Strip Word Html Tags

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; 
} 

,但我要的是:如果我打表的标签,应立即停止更换,直到达到一个表结束标签。可能吗?

+0

我给出一个解决方案,但现在,我再想一想,是删除和格式化这个词,只是不停的文字...我不知道,如果是你的样子因为,但HTMLAgilityPack的使用是这个想法。 – Aristos 2012-07-06 08:41:24

+0

我的定制者希望不要触摸桌子标签内的所有东西,但其他所有东西都应该剥离。它不是我正在寻找的解决方案 – Timsen 2012-07-06 08:44:46

+0

看看HTMLAgilityPack,这是个想法,这可以给你DOM,并从那里你可以保留你想要的部分。 – Aristos 2012-07-06 08:45:33

回答

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; 
} 
+0

除了遍历所有子节点并添加到字符串构建之外,您可以返回根节点的innerttext。 – jnoreiga 2012-09-18 19:00:11

+0

@jnoreiga谢谢你的纠正。 – Aristos 2012-09-18 20:32:04

+1

没问题。这不会仅仅去除单词样式。它将去除根目录中的所有html。 – jnoreiga 2012-09-19 17:42:05