views:

694

answers:

1

The example on codeplex is this :

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

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.

+1  A: 

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

            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;
            }
        }
arri.me
I think you could also select the actual attribute nodes: `"//@background|//@lowsrc|//@src|//@href` and modify their `Value` properties directly. You would spare yourself the cascade of `if` statements.
Tomalak
i'll try that out. thanks
arri.me