wang_peng1 发表于 2013-2-7 17:12:48

去除html标记,以及文件的读写

今天在去字段的时候 把标记取出来了没有办法 只能去除
下面引用别人的文章
http://www.cnblogs.com/zoupeiyang/archive/2009/06/22/1508039.html
/// <summary>
/// 去除HTML标记
/// </summary>
/// <param name="Htmlstring">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public string NoHTML(string Htmlstring)
{
   //删除脚本
   Htmlstring = Htmlstring.Replace("\r\n","");
   Htmlstring = Regex.Replace(Htmlstring,@"<script.*?</script>","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"<style.*?</style>","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"<.*?>","",RegexOptions.IgnoreCase);
   //删除HTML
   Htmlstring = Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(quot|#34);","\"",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(amp|#38);","&",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(lt|#60);","<",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(gt|#62);",">",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(nbsp|#160);","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(iexcl|#161);","\xa1",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(cent|#162);","\xa2",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(pound|#163);","\xa3",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&(copy|#169);","\xa9",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"&#(\d+);","",RegexOptions.IgnoreCase);
   Htmlstring = Htmlstring.Replace("<","");
   Htmlstring = Htmlstring.Replace(">","");
   Htmlstring = Htmlstring.Replace("\r\n","");   
   Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
   return Htmlstring;
}
http://www.cnblogs.com/happyday56/archive/2009/04/25/1443526.html 这篇也不错
http://www.cnblogs.com/yjwgood/articles/756717.html 这篇如下:
使用 axWebBrowser 控件
引用 mshtml
[1)去掉HTML标记及其标记中的属性
[2)axWebBrower 打开某个页面
[3)取出HTML源代码

1)去掉HTML标记及其标记中的属性
private string getOneValue(string TempStr)
{
   if(TempStr.Length >0)
   {
    TempStr = regularExpressionsOfHTML(TempStr);
    TempStr = TempStr.Substring(0,TempStr.Length-1);
   }
   return TempStr;
}
public static string regularExpressionsOfHTML(string TempContent)
{
   //TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]+>",""); //任意多个
   TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]*>",""); //匹配一个
   return TempContent;
}

2)axWebBrower 打开某个页面
   string Url = "**********";
   object Zero = 0;
   object EmptyString = "";

   axWebBrowser.Navigate(Url ,ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);

3)取出HTML源代码
   在axWebBrower_DocumentComplete事件中比较好
   引用:using mshtml;

   IHTMLDocument2 HTMLDocument =(IHTMLDocument2) axWebBrowser1.Document;
    string strHtml = HTMLDocument.body.innerHTML.ToString(); //Get HTML
    string[] arHtml = strHtml.Split('\n');
   此时arHtml中保存了所有的HTML source.
我把我自己做的调试程序上传了,这样的文章不能算原创也不算自己的 还是写转载吧
代码可是我的啊里面有测试字符串的 还有测试文件的用到了文件的读和写。
页: [1]
查看完整版本: 去除html标记,以及文件的读写