The example for altering links in HtmlAgilityPack does not work. How do I go about doing this?

c# html-agility-pack uri xpath


The example on codeplex is this :

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

The first issue is HtmlDocument.DocumentElement does not exist! What does exist is HtmlDocument.DocumentNode but even when I use that instead, I'm unable to access the href attribute as described. I get the following error:

Cannot apply indexing with [] to an expression of type 'HtmlAgilityPack.HtmlNode'

Here's the code I'm trying to compile when I get this error:

private static void ChangeUrls(ref HtmlDocument doc)
    foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//@href"))
        HtmlAttribute attr = link["href"];
        attr.Value = Rewriter(attr.Value);

UPDATE: I Just found that the example was never meant to work...And I've got a solution after reading the example code...I'll post my solution for other people like me to enjoy once completed.

10/5/2009 12:55:48 AM

Accepted Answer

Here's my quick solution based on portions of the sample code included in the ZIP.

private static void ChangeLinks(ref HtmlDocument doc)
            if (doc == null) return;
            //process all tage with link references
            HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//*[@background or @lowsrc or @src or @href]");
            if (links == null)

            foreach (HtmlNode link in links)

                if (link.Attributes["background"] != null)
                    link.Attributes["background"].Value = _newPath + link.Attributes["background"].Value;
                if (link.Attributes["href"] != null)
                    link.Attributes["href"].Value = _newPath + link.Attributes["href"].Value;(link.Attributes["href"] != null)
                    link.Attributes["lowsrc"].Value = _newPath + link.Attributes["href"].Value;
                if (link.Attributes["src"] != null)
                    link.Attributes["src"].Value = _newPath + link.Attributes["src"].Value;
10/7/2009 3:17:35 PM

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