HtmlAgilityPack The exception "object not assigned to an instance" is thrown by HtmlDocument.Load.

c# html-agility-pack


var uri = new Uri("");
var request = (HttpWebRequest)WebRequest.Create(url);
var cookieContainer = new CookieContainer();

request.CookieContainer = cookieContainer;
request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: Gecko/20091102 Firefox/3.5.5";
request.Method = "GET";
request.AllowAutoRedirect = true;
request.Timeout = 15000;

var response = (HttpWebResponse)request.GetResponse();
var page = new HtmlDocument();
var stream = response.GetResponseStream();

Causes an error. on the Load(stream) call. Any ideas?

3/18/2011 8:59:01 AM

Accepted Answer

The error I get when I run your code is:

System.ArgumentException: 'ISO-8559-1' is not a supported encoding name.

It's thrown by the standard .NET Framework encoding classes. It means the page declares an encoding not supported by .NET. I fixed it like this:

var page = new HtmlDocument();
page.OptionReadEncoding = false;

PS: I'm using the Html Agility Pack version 1.3

3/18/2011 12:54:26 PM

Popular Answer

Maybe not a the answer you need, but the stack trace indicates the exception occurred after you past the response stream type to the page's load method.
It might be worth adding a TextReader in before the htmldocument assignment, and pass off the stream object to that. Then pass the textreader var to htmldoc's Load method.

Before you debug the source code for latest htmlagility, I think you should first edit your question to include the states of all types/props of interest, for clarity.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow