使用 Html Agility Pack 我一直在尝试计算每个div标签中的段落标记数量,并获得段落数量最多的div ID和类(如果存在)我的语法有问题。
我的代码如下所示:
// HtmlDocument is stored in doc
HtmlAgilityPack.HtmlNodeCollection div = doc.DocumentNode.SelectNodes("//div");
foreach (HtmlAgilityPack.HtmlNode divNode in div)
{
var x = divNode.DescendantNodes("p").Count; // doesn't actually work
// x should also be stored in a list
}
如果有人能指出我正确的方向或向我提供示例,那将会非常有帮助。谢谢!
答案 0 :(得分:0)
这样怎么样:
//get the maximum number of paragraph
int maxNumberOfParagraph =
doc.DocumentNode
.SelectNodes("//div[.//p]")
.Max(o => o.SelectNodes(".//p").Count);
//get divs having number of containing paragraph equals maxNumberOfParagraph
var divs = doc.DocumentNode
.SelectNodes("//div[.//p]")
.Where(o => o.SelectNodes(".//p").Count == maxNumberOfParagraph);