OA系统中将需要发布的信息通过WebService发布到外网,前提是网站不是公司的产品,设计上与我们存在很多的不同,在信息上报的时候,总是出现格式问题,比如手段空格等等。
解决思路:第一:首先清理标签, 由于OA中编辑的文档保存了html标签,我们通过清理标签,但保留<p><br><div>标签
第二:清理空格
第三 :清除保留标签中的样式
第四: 用<p>标记替换保留标签
public string Filter(string htmlCode)
{
string Content = string.Empty;
// 保留<p></p><br></br><div>标签
Content = Regex.Replace(htmlCode, @"<(?!p|\/p|br|\/br|div|\/div)[^>]+>", "", RegexOptions.IgnoreCase);
// 除去空格
Content = Regex.Replace(Content, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
// 清除样式
Content = Regex.Replace(Content, "style\\s?=\\s?(['\"][^'\"]*?['\"]|[^'\"]\\S*)", "", RegexOptions.IgnoreCase);
// 将其他标签替换成<p>标签,注意标签开和闭
string[] tags = { "p", "div", "span", "font", "br" };
foreach (string tag in tags)
{
Content = Regex.Replace(Content, "<" + tag + "[^>]*>", "<p style='text-indent:2em'>", RegexOptions.IgnoreCase);
Content = Regex.Replace(Content, "</" + tag + "[^>]*>", "</p>", RegexOptions.IgnoreCase);
}
return Content;
}