-1

I am trying to get the value of a nested div using HtmlAgilitypack.my html code is like this :

enter image description here

here is my code :

        var val = resultat1.DocumentNode.SelectSingleNode("//div[@class='journal-content-article']/div[@class='news_page max']/div[@class='news_page_wrapper']/div[@class='news_page_content']/div[@class='news_content']/div[@class='news_content_container']").InnerText;

I need the innertext of news_content_container .but this line of code return Object reference not set to an instance of an object

This returns error:

        var val = resultat1.DocumentNode.SelectSingleNode("//div[@class='journal-content-article']/div[@class='news_page max']").InnerText;

This returns value but not expected value:

        var val = resultat1.DocumentNode.SelectSingleNode("//div[@class='journal-content-article']").InnerText;
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • 1
    The object reference not set error is obviously because SelectSingleNode returns null and you try to get InnerText property. You should try selecting the element step by step to find the problem. – Mehrzad Chehraz Jan 17 '16 at 06:19
  • @MehrzadChehraz yes you are right but i use the xpath to get the element value – Ehsan Akbar Jan 17 '16 at 06:22
  • Okey, but you need to debug by trying to select the element step by step to find where SelectSingleNode returns null. It may be a typo issue or something. – Mehrzad Chehraz Jan 17 '16 at 06:25
  • I updated the post ,as i said the second div returns null reference ,my question is why ? – Ehsan Akbar Jan 17 '16 at 06:26

1 Answers1

0

A lot of times the HTML content of a page is not well-formed. Inspecting with browser does not show the actual HTML received from the server. You can find the actual HTML in Source tab of inspector.

You need to fix not well-formed HTML before passing it to HtmlAgilitypack. Here is a site you can use (You can use by sending HTTP Post to site and get the "fine html" from response)