HtmlAgilityPack selecting childNodes not as expected

后端 未结 3 1791
北海茫月
北海茫月 2021-02-05 00:56

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 Html

相关标签:
3条回答
  • 2021-02-05 01:25

    Also, Watch out for Null Check. SelectNodes returns null instead of blank collection.

    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]");**   
         }
      }
    }
    
    0 讨论(0)
  • 2021-02-05 01:29

    With an xpath query you can also use "." to indicate the search should start at the current node.

    HtmlNode imageNode = linkNode.SelectSingleNode(".//img[@alt]");
    
    0 讨论(0)
  • 2021-02-05 01:35

    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]");
    
    0 讨论(0)
提交回复
热议问题