HtmlAgilityPack and selecting Nodes and Subnodes

前端 未结 5 1456
轻奢々
轻奢々 2021-01-31 02:12

Hope somebody can help me.

Let´s say I have a html document that contains multiple divs like this example:

5条回答
  •  终归单人心
    2021-01-31 03:00

    Shame on me :)

    All of you were right.

    I found the problem. This NullReferenceException kept nagging me so I spent more time to look at it in detail. In between all those divs there was one div with the same "class='search-hit'" attribute but without the spans inside. Thats why it throughs an error at the second loop.

    foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//span[@prop]/ancestor::div[@class='search_hit']"))
       {
            Record rec = new Record();
            foreach (HtmlAgilityPack.HtmlNode node2 in node.SelectNodes(".//span[@prop]"))
               {
               }
               rList.Results.Add(rec);
       }
    

    The code above is working.

    Thank you guys for your time and help!

提交回复
热议问题