【发布时间】:2015-11-19 13:21:55
【问题描述】:
在 HtmlAgilityPach 中,当我像这样选择一个节点时:
var node1 = htmlDoc.GetElementbyId("some_id");
我想在其子项中获取所有子“a”标签。但是,这不起作用,因为它返回 null:
foreach (var childItem in node1.ChildNodes) {
var a = childItem.SelectNodes("a") // null
var a = childItem.SelectNodes("/a") // null
var a = childItem.SelectNodes("//a") // not null but select all the "a" tags on the whole(!) page, not only the ones within current childItem
}
如您所见,最后一个方法选择了整个(!)页面上的所有“a”标签,而不仅仅是当前 childItem 中的标签。我想知道为什么以及如何让它只选择“childNode”中的那些?
【问题讨论】:
-
试试 childItem.DocumentNode.SelectNodes("//a[@href]");
标签: c# html parsing html-agility-pack