HtmlAgilityPack选择子节点并不如预期 [英] HtmlAgilityPack selecting childNodes not as expected

查看:164
本文介绍了HtmlAgilityPack选择子节点并不如预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用HtmlAgilityPack库来解析页面中的一些链接,但我没有看到我期​​望从方法的结果。下面我有一个链接HtmlNodeCollection。对于每一个环节我想检查是否有一个形象节点,然后分析它attribures但和的SelectNodes方法的SelectSingleNode的linkNode似乎是搜索父文档不linkNode的子节点怎么办?

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?

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

有没有其他办法,我能得到linkNode形象childnode的alt属性,如果它存在?

Is there any other way I could get the alt attribute of the image childnode of linkNode if it exists?

推荐答案

您应该删除从forwardslash preFIX/ IMG [@alt],因为它意味着你想在文档的根开始。

You should remove the forwardslash prefix from "/img[@alt]" as it signifies that you want to start at the root of the document.

HtmlNode imageNode = linkNode.SelectSingleNode("img[@alt]");

这篇关于HtmlAgilityPack选择子节点并不如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆