当前位置: 代码迷 >> ASP.NET >> 怎么去掉html标签
  详细解决方案

怎么去掉html标签

热度:4883   发布时间:2013-02-25 00:00:00.0
如何去掉html标签?
比如
<a   href= "http://www.csdn.net/ "> CSDN </a>
得到的就是CSDN.
<td   align= "right "> 所属论坛: </td>
得到的就是:
所属论坛

------解决方案--------------------------------------------------------
string str = @ " <a href= "http://www.csdn.net/ "> CSDN </a> "
while (str.IndexOf( " < ") > = 0)
{
str = str.Remove(str.IndexOf( " < "), str.IndexOf( "> ") - str.IndexOf( " < ") + 1);
}
我都是这样做的 - -!
------解决方案--------------------------------------------------------
用 xmlDom分析好了
<a href= "http://www.csdn.net/ "> CSDN </a> 中的CSDN和 <td align= "right "> 所属论坛: </td>
中的所属论坛
不就是A这个节点的value和 <td> 这个节点的value;
大致代码如下//我空写的,具体自己写
XMLDom xml = new XMLDom(xmlFilePath);
Collection <string> valueList = new Collection <string> ();
foreach(XmlNode node in xml.ElementRoot)
{
valueList.Add(node.Value);
}
return valueList()// 返回值列表

注意 我这里只遍历了第一层node,你需要深度便利所有node也就是低归的调用
foreach(XmlNode node in xml.ElementRoot)
{
valueList.Add(node.Value);
}

------解决方案--------------------------------------------------------
推荐使用xmlDom
------解决方案--------------------------------------------------------
这样
string strHTML = "A <TD> <A> DDDDDDD </A> <TD> CCCCCCC </TD> ";
string sText= System.Text.RegularExpressions.Regex.Replace(strHTML, " <[^> ]*?> ", " ");
Response.Write(sText);
------解决方案--------------------------------------------------------
MARK
------解决方案--------------------------------------------------------
string newText = Regex.Replace(s, " <[^> ]*?> ", " ");

可以应该就可以现实了
------解决方案--------------------------------------------------------
public static string RepalceHtml(string strHtml)
{
string regExValue = " <.+?> ";
Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
return regex.Replace(strHtml, " ");
}
  相关解决方案