如何从html中删除特定标签 [英] how can I remove with specific tags from html

查看:146
本文介绍了如何从html中删除特定标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

如何使用HTML敏捷包

我有html代码如下:

I have html code below:

<div><span class="help">This is text.</span>Hello, this is text.</div>
<div>I have a question.<span class="help">Hi</span></div>

现在,我想删除介于< span class = 帮助>< / span> 使用C#。因此,我只想离开

Now, I want to remove text which is between <span class="help"></span> using C#. So, I want to leave only

<div>Hello, this is text.</div>
<div>I have a question.</div>

任何人有任何想法?

Anyone has any idea?

推荐答案

您应该使用 Html Agility Pack 来处理html。

You should use Html Agility Pack to work with html.

string text = @"<div><span class=""help"">This is text.</span>Hello, this is text.      </div>
                <div>I have a question.<span class=""help"">Hi</span></div>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
var nodes = doc.DocumentNode.SelectNodes("//span[@class='help']");
foreach( HtmlNode node in nodes)
{
   node.Remove();
} 
String result = doc.DocumentNode.InnerHtml;

这篇关于如何从html中删除特定标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆