HtmlAgilityPack in C# showing "A first chance exception of type 'System.UriFormatException' occurred in System.dll"

c# html-agility-pack

Question

I have downloaded the following page using wget and saved it in f.html

http://www.ebay.com/sch/i.html?_nkw=gruen&_in_kw=1&_ex_kw=sara+quartz+embassy+bob+robert+elephants+adidas&_sacat=See-All-Categories&_okw=gruen&_oexkw=sara+quartz+embassy+bob+robert+elephants+adidas&_adv=1&_udlo=&_udhi=&_LH_Time=1&_ftrt=903&_ftrv=24&_sabdlo=&_sabdhi=&_samilow=&_samihi=&_sadis=200&_fpos=Zip+code&_fsct=&LH_SALE_CURRENCY=0&_sop=12&_dmd=1&_ipg=50

Now I want to load this page for parsing using HtmlAgilityPack in C#. Using this code snippet

var webGet = new HtmlWeb();
var document = webGet.Load("f.html");

Second line is throwing this error

A first chance exception of type 'System.UriFormatException' occurred in System.dll

What's the solution ??

Accepted Answer

I don't have the compiler at hand but I suppose that "f.html" is not a well formed Uri. It lacks the schema and the domain.

The correct uri should be like "http://the.domain.name/f.html".


Popular Answer

Try the following:

var url = "http://www.ebay.com/sch/i.html?_nkw=gruen&_in_kw=1&_ex_kw=sara+quartz+embassy+bob+robert+elephants+adidas&_sacat=See-All-Categories&_okw=gruen&_oexkw=sara+quartz+embassy+bob+robert+elephants+adidas&_adv=1&_udlo=&_udhi=&_LH_Time=1&_ftrt=903&_ftrv=24&_sabdlo=&_sabdhi=&_samilow=&_samihi=&_sadis=200&_fpos=Zip+code&_fsct=&LH_SALE_CURRENCY=0&_sop=12&_dmd=1&_ipg=50";
var document = new HtmlDocument();
document.LoadHtml(new WebClient().DownloadString(url));

If you want to load it from a local file then try:

var file = "f.html";
var document = new HtmlDocument();
document.LoadHtml(File.ReadAllText(file));



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why