HtmlAgilityPack in C# demonstrates "A type 'System' first-chance exception. In System.dll, a UriFormatException occurred."

c# html-agility-pack

Question

Using wget, I grabbed the following page and saved it as 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

I'm going to load this website in C# and use HTMLAgilityPack to parse it. Using this line of code

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

This error is being thrown on line two.

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

What is the remedy?

1
0
2/26/2012 6:32:26 PM

Accepted Answer

I'm not in possession of the compiler, but I presume that"f.html" is not a coherent Uri. Both the schema and the domain are missing.

The proper uri should be as follows."http://the.domain.name/f.html" .

2
2/26/2012 6:36:27 PM

Popular Answer

Try these things:

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));

Try the following if you wish to load it from a local file:

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


Related Questions





Related

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