Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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."
C#
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
Updated 5-Dec-12 7:41am
v2

1 solution

I think there's no HTML-element with your search result as class in your content_wrapper element.
 
Share this answer
 
Comments
Anselmo santos 6-Dec-12 10:47am    
How do I solve that issue people?
Thomas Daniels 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900