Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# HTML HTTP Parsing
I have the following code in C# but it is not working. I am new to the whole C# and parsing thing but I need to get this working. Anyone knows why i am getting:"Object reference not set to an instance of an object."
using System.Text;
using HtmlAgilityPack;
 
namespace HtmlParser
{
    class Program
    {
        static void Main(string[] args)
        {
            // The HtmlWeb class is a utility class to get the HTML over HTTP
            HtmlWeb htmlWeb = new HtmlWeb();
 
            // Creates an HtmlDocument object from an URL
            HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("http://allrecipes.com/search/default.aspx?w0=chicken&w1=beef");
 
            // Targets a specific node
            HtmlNode content_wrapper = document.GetElementbyId("content-wrapper");
 
            System.Console.WriteLine(content_wrapper.ToString());
            HtmlNodeCollection search_results = content_wrapper.SelectNodes("//div[@class='searchResult']");
            foreach (HtmlNode result in search_results)
            {
                string recipe_name = result.SelectSingleNode("*[@class='resultTitle']").InnerText;// error appears here
                System.Console.WriteLine(recipe_name);
            }
            System.Console.ReadKey();
        }
    }
}
[Edit]Code block added[/Edit]
Posted 5-Dec-12 8:38am
Edited 5-Dec-12 8:41am
ProgramFOX131.6K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think there's no HTML-element with your search result as class in your content_wrapper element.
  Permalink  
Comments
Anselmo santos at 6-Dec-12 10:47am
   
How do I solve that issue people?
ProgramFOX at 6-Dec-12 11:12am
   
Try another search than the search you tried.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100