C# HtmlAgilityPack 选择的 childNodes 不符合预期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/857198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HtmlAgilityPack selecting childNodes not as expected
提问by Sheff
I am attempting to use the HtmlAgilityPack library to parse some links in a page, but I am not seeing the results I would expect from the methods. In the following I have a HtmlNodeCollection of links. For each link I want to check if there is an image node and then parse its attribures but the SelectNodes and SelectSingleNode methods of linkNode seems to be searching the parent document not the childNodes of linkNode what gives?
我正在尝试使用 HtmlAgilityPack 库来解析页面中的一些链接,但我没有看到我期望从这些方法中得到的结果。在下面我有一个 HtmlNodeCollection 链接。对于每个链接,我想检查是否有图像节点,然后解析其属性,但是 linkNode 的 SelectNodes 和 SelectSingleNode 方法似乎正在搜索父文档而不是 linkNode 的 childNodes 给出了什么?
HtmlDocument htmldoc = new HtmlDocument();
htmldoc.LoadHtml(content);
HtmlNodeCollection linkNodes = htmldoc.DocumentNode.SelectNodes("//a[@href]");
foreach(HtmlNode linkNode in linkNodes)
{
string linkTitle = linkNode.GetAttributeValue("title", string.Empty);
if (linkTitle == string.Empty)
{
HtmlNode imageNode = linkNode.SelectSingleNode("/img[@alt]");
}
}
Is there any other way I could get the alt attribute of the image childnode of linkNode if it exists?
如果存在,还有其他方法可以获得linkNode的图像子节点的alt属性吗?
采纳答案by Richard Szalay
You should remove the forwardslash prefix from "/img[@alt]" as it signifies that you want to start at the root of the document.
您应该从“/img[@alt]”中删除正斜杠前缀,因为它表示您希望从文档的根开始。
HtmlNode imageNode = linkNode.SelectSingleNode("img[@alt]");
回答by msqr
Also, Watch out for Null Check. SelectNodes returns null instead of blank collection.
另外,注意空检查。SelectNodes 返回 null 而不是空白集合。
HtmlNodeCollection linkNodes = htmldoc.DocumentNode.SelectNodes("//a[@href]");
**if(linkNodes!=null)**
{
foreach(HtmlNode linkNode in linkNodes)
{
string linkTitle = linkNode.GetAttributeValue("title", string.Empty);
if (linkTitle == string.Empty)
{
**HtmlNode imageNode = linkNode.SelectSingleNode("img[@alt]");**
}
}
}
回答by ulty4life
With an xpath query you can also use "." to indicate the search should start at the current node.
对于 xpath 查询,您还可以使用“.”。指示搜索应从当前节点开始。
HtmlNode imageNode = linkNode.SelectSingleNode(".//img[@alt]");