今天突然被问到   从数据库里读出来数据时有的时候会带有HTML的标签,而且如果把数据装在TextBox这类的容器里的时候标签会成为字符串输出来,这样肯定不是自己要的效果,那么如何去掉这些个HTML标签嘞!

 1 protected void Button1_Click(object sender, EventArgs e)  
 2        {  
 3 string str=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;  
 4          using (SqlConnection sqlcon = new SqlConnection(str))  
 5           {  
 6              using (SqlCommand sqlcom = sqlcon.CreateCommand())  
 7              {  
 8                   sqlcom.CommandText = "select * from T_Blog where Uname='Mary'";  
 9                 sqlcon.Open();  
10                   SqlDataReader reader =  sqlcom.ExecuteReader();  
11                    if (reader.Read())  
12                   {  
13   
14                    string content = reader["Bcontent"].ToString();  
15                        //声明一个不可变的正则表达式  
16    Regex regex = new Regex("<.+?>");   
17                 content = regex.Replace(content, "");  
18                 this.TextBox1.Text = content;  
19             }  
20         }  
21     }  
22 }  
posted on 2012-07-05 13:31  杨斐_Feil  阅读(178)  评论(0编辑  收藏  举报