views:

53

answers:

1

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.

A: 

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.

Rohit Agarwal