Skip to content Skip to sidebar Skip to footer

HtmlAgilityPack Example For Changing Links Doesn't Work. How Do I Accomplish This?

The example on codeplex is this : HtmlDocument doc = new HtmlDocument(); doc.Load('file.htm'); foreach(HtmlNode link in doc.DocumentElement.SelectNodes('//a[@href']) { HtmlA

Solution 1:

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

Post a Comment for "HtmlAgilityPack Example For Changing Links Doesn't Work. How Do I Accomplish This?"