使用HTML Agility Pack删除重复元素链 [英] Remove chain of duplicate elements with HTML Agility Pack

查看:68
本文介绍了使用HTML Agility Pack删除重复元素链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除任何< br> 标签在我的html文档中.到目前为止,这是我想出的(真的是愚蠢的代码):

I'm trying to remove any duplicate or more occurrences of any < br > tags in my html document. This is what I've come up with so far (really stupid code):

HtmlNodeCollection elements = nodeCollection.ElementAt(0)
                             .SelectNodes("//br");

if (elements != null)
{
    foreach (HtmlNode element in elements)
    {
        if (element.Name == "br")
        {
             bool iterate = true;
             while(iterate == true)
             {
                 iterate = removeChainElements(element);
             }
         }
     }
}

private bool removeChainElements(HtmlNode element)
{
    if (element.NextSibling != null && element.NextSibling.Name == "br")
    {
        element.NextSibling.Remove();
    }
    if (element.NextSibling != null && element.NextSibling.Name == "br")
         return true;
    else
         return false;
    }
}

该代码确实找到了 br 标签,但根本没有删除任何元素.

The code does find the br tags but it doesn't remove any elements at all.

推荐答案

我认为您的解决方案太复杂了,尽管据我所知这似乎是正确的.

I think you too complicated your solution, although the idea is seems to be correct, as I understand.

假设,首先找到所有<br />节点,然后删除先前同级为<br />节点的节点,会更容易.

Suppose, it would be easier to find all the <br /> nodes first, and just remove those, whose previous sibling is <br /> node.

让我们从下一个示例开始:

Let's start with the next example:

var html = @"<div>the first line<br /><br />the next one<br /></div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);

现在找到<br />个节点并删除重复元素链:

now find <br /> nodes and remove the chain of duplicate elements:

var nodes = doc.DocumentNode.SelectNodes("//br").ToArray();
foreach (var node in nodes)
    if (node.PreviousSibling != null && node.PreviousSibling.Name == "br")
        node.Remove();

并获取结果:

var output = doc.DocumentNode.OuterHtml;

是:

<div>the first line<br>the next one<br></div>

这篇关于使用HTML Agility Pack删除重复元素链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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