Help with HTML Agility Pack

c# html-agility-pack


I'm trying to scrape some information from a website but can't find a solution that works for me. Every code I read on the Internet generates at least one error for me.

Even the example code at their homepage generates errors for me.

My code:

         HtmlDocument doc = new HtmlDocument();
         foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
            HtmlAttribute att = link["href"];
            att.Value = FixLink(att);

Generates the following error:

'HtmlDocument' is an ambiguous reference between 'System.Windows.Forms.HtmlDocument' and 'HtmlAgilityPack.HtmlDocument' C:*\Form1.cs

Edit: My entire code is located here:

All help is very appreciated!

10/19/2010 1:31:29 PM

Accepted Answer

Use HtmlAgilityPack.HtmlDocument:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

The compiler is getting confused because two of the namespaces you have imported with using contain classes called HtmlDocument - the HTML Agility Pack namespace, and the Windows Forms namespace. You can get around this by specifying which class you want to use explicitly.

10/18/2010 8:47:52 PM

Popular Answer

this is how i achieved. Note that there is a code error given in main Html Agility Pack Example in foreach line doc.DocumentElement.SelectNodes("//a[@href"]). The correct and tested one is given below.

 HtmlWeb hw = new HtmlWeb();

    HtmlDocument doc = hw.Load(@"");
    StringBuilder sb = new StringBuilder();

    List<string> lstHref = new List<string>();

    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]").Distinct())
        string curHref = link.Attributes["href"].Value;


    foreach (string str in lstHref)
        sb.Append(str +"<br />");

    Response.Write (sb.ToString());

Since it got working for me, I thought I should share.

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