C#将html代码转换成文本代码

 1 /// <summary>
 2     /// 去除HTML标记 
 3     /// </summary>
 4     /// <param name="strHtml">包括HTML的源码 </param>
 5     /// <returns>已经去除后的文字</returns>
 6     public static string StripHTML(string strHtml)
 7     {
 8         string[] aryReg = { @"<script[^>]*?>.*?</script>", @"<(/s*)?!?((w+:)?w+)(w+(s*=?s*(([""'])(\[""'tbnr]|[^7])*?7|w+)|.{0})|s)*?(/s*)?>", @"([
])[s]+", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);", @"&#(d+);", @"-->", @"<!--.*
" };
 9         string[] aryRep = { "", "", "", """, "&", "<", ">", " ", "xa1", "xa2", "xa3", "xa9", "", "
", "" };
10         string newReg = aryReg[0];
11         string strOutput = strHtml;
12         for (int i = 0; i < aryReg.Length; i++)
13         {
14             Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
15             strOutput = regex.Replace(strOutput, aryRep[i]);
16         }
17         strOutput.Replace("<", ""); strOutput.Replace(">", "");
18         strOutput.Replace("
", ""); return strOutput;
19     }
原文地址:https://www.cnblogs.com/myselfyourself/p/3992839.html