How to get count number of SelectedNode with XPath in C#?

♀尐吖头ヾ 提交于 2019-12-12 18:17:39

问题


I am using HTMLAgilityPack in my application, and i want to get the item(node) count of SelectedNodes as the code below:

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']");

At the first, getting a href was successfully running, but second one i prefer to get special class named "o" there was en error.

I want to use .Count but got an error. Also foreach loop running for the firs selectedNodes which i request a tags href attribute. And didn't run for class filtering.

Hot to get count of doc.DocumentNode.SelectNodes("//div[@class='o']")

Thank you, Caglar


回答1:


I don't know the specific way in HTMLAgilityPack but in plain XPath you can do this:

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

You properly can't use this XPath in .SelectNodes but there should be a method like Evaluate, SelectSingle or SelectAtom.



来源:https://stackoverflow.com/questions/3575229/how-to-get-count-number-of-selectednode-with-xpath-in-c

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