HTMLAgilityPack, HTML duplicate IDs

我与影子孤独终老i 提交于 2019-12-11 10:35:39

问题


Hi: This is similar to this one here. But needs to be done at the server level rather at the client level. Currently I use HTMLAgilityPack, is there anyway I could detect duplicate IDs? Thanks in advance.


回答1:


Here's a quick way to do it:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);

var count = new Dictionary<string, int>(); 

foreach (var node in doc.DocumentNode.Descendants())
{
    string id = node.GetAttributeValue("id", null);
    if (id != null)
    {
        if (count.ContainsKey(id)) count[id] += 1;
        else count.Add(id, 1); 
    }
}

var duplicates = count.Where( id => id.Value > 1 );

This basically parses the whole document keeping track of count in a Hash.



来源:https://stackoverflow.com/questions/2693350/htmlagilitypack-html-duplicate-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!