.net去掉HTML标记

Posted 坐峰怀雪灬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net去掉HTML标记相关的知识,希望对你有一定的参考价值。

 1 using System.Text.RegularExpressions;
 2 
 3 /// <summary>
 4 
 5   /// 去除html标记
 6 
 7   /// </summary>
 8 
 9   /// <param name="DeleteHTML">包括HTML的源码 </param>
10 
11   /// <returns>已经去除Html标签后的文字</returns>
12 
13   public static string  DeleteHTML(string Htmlstring)
14 
15   { 
16 
17    //删除脚本
18 
19    Htmlstring = Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase);
20 
21    //删除HTML
22 
23    Htmlstring = Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);
24 
25    Htmlstring = Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase);
26 
27    Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase);
28 
29    Htmlstring = Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase);
30 
31    
32 
33    Htmlstring = Regex.Replace(Htmlstring,@"&(quot|#34);","\"",RegexOptions.IgnoreCase);
34 
35    Htmlstring = Regex.Replace(Htmlstring,@"&(amp|#38);","&",RegexOptions.IgnoreCase);
36 
37    Htmlstring = Regex.Replace(Htmlstring,@"&(lt|#60);","<",RegexOptions.IgnoreCase);
38 
39    Htmlstring = Regex.Replace(Htmlstring,@"&(gt|#62);",">",RegexOptions.IgnoreCase);
40 
41    Htmlstring = Regex.Replace(Htmlstring,@"&(nbsp|#160);"," ",RegexOptions.IgnoreCase);
42 
43    Htmlstring = Regex.Replace(Htmlstring,@"&(iexcl|#161);","\xa1",RegexOptions.IgnoreCase);
44 
45    Htmlstring = Regex.Replace(Htmlstring,@"&(cent|#162);","\xa2",RegexOptions.IgnoreCase);
46 
47    Htmlstring = Regex.Replace(Htmlstring,@"&(pound|#163);","\xa3",RegexOptions.IgnoreCase);
48 
49    Htmlstring = Regex.Replace(Htmlstring,@"&(copy|#169);","\xa9",RegexOptions.IgnoreCase);
50 
51    Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);","",RegexOptions.IgnoreCase);
52 
53    Htmlstring.Replace("<","");
54 
55    Htmlstring.Replace(">","");
56 
57    Htmlstring.Replace("\r\n","");
58 
59    Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
60 
61    return Htmlstring;
62 
63   }

 

以上是关于.net去掉HTML标记的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core标记帮助器——替换元素名称

asp.net页面实用代码片段

CSS 显示代码片段:使用PRE标记包裹长行

推荐net开发cad入门阅读代码片段

在Javascript中从字符串中去掉html标记

带有标记的Android谷歌地图片段