HTMLAgilityPack,HTML重复ID [英] HTMLAgilityPack, HTML duplicate IDs

查看:90
本文介绍了HTMLAgilityPack,HTML重复ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:这和这个这里

一>。但需要在服务器级而不是客户级完成。目前我使用HTMLAgilityPack,无论如何,我可以检测到重复的ID?

解决方案

这是一个快速的方法:

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

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

foreach(doc.documentNode.Descendants()中的var节点)
{
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);

这基本上解析了整个文档,在Hash中跟踪计数。

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.

解决方案

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.

这篇关于HTMLAgilityPack,HTML重复ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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