当前位置:编程学习 > asp >>

asp.net如何去掉HTML标记

今天突然被问到   从数据库里读出来数据时有的时候会带有HTML的标签,而且如果把数据装在TextBox这类的容器里的时候标签会成为字符串输出来,这样肯定不是自己要的效果,那么如何去掉这些个HTML标签嘞!
[csharp]
protected void Button1_Click(object sender, EventArgs e) 
       { 
string str=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString; 
           using (SqlConnection sqlcon = new SqlConnection(str)) 
           { 
               using (SqlCommand sqlcom = sqlcon.CreateCommand()) 
               { 
                   sqlcom.CommandText = "select * from T_Blog where Uname='Mary'"; 
                   sqlcon.Open(); 
                   SqlDataReader reader =  sqlcom.ExecuteReader(); 
                   if (reader.Read()) 
                   { 
 
                       string content = reader["Bcontent"].ToString(); 
                        //声明一个不可变的正则表达式 
[csharp]
                Regex regex = new Regex("<.+?>");  
                content = regex.Replace(content, ""); 
                this.TextBox1.Text = content; 
            } 
        } 
    } 

使用的正则“<.+?>”

 


摘自 杨斐的专栏

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,