How do I count the number of SelectedNodes in C# using XPath?

c# count html-agility-pack xpath

Question

I'm using HTMLAgilityPack in my application, and the code below is what I need to retrieve the item (node) count of SelectedNodes:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(webBrowser1.DocumentText);
var tagListe = doc.DocumentNode.SelectNodes("//a[@href]");
var divListe = doc.DocumentNode.SelectNodes("//div[@class='o']");

A href was successfully obtained in the first instance, but an error occurred when I attempted to get a particular class called "o" in the second.

I attempted to use.Count but received an error. For the first selectedNodes for which I request a tags href property, there is also a foreach loop going. and did not attempt to filter classes.

getting count of hotdoc.DocumentNode.SelectNodes("//div[@class='o']")

I'm grateful. Caglar

1
2
8/26/2010 1:01:27 PM

Accepted Answer

I'm not aware of the precise procedure.HTMLAgilityPack Nevertheless, you can achieve this with normal XPath:

count(//div[@class='o'])

This XPath shouldn't be used in.SelectNodes but there need to be a technique likeEvaluate , SelectSingle or SelectAtom .

0
7/23/2011 8:53:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow