HtmlAgilityPack to replacechild ,why it does not work?

c# html-agility-pack


I implement a feature to request "" page.
When I use HtmlAgilityPack to replace more HtmlNode, but had some occur confuse things-----It can not replace.

The code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;//I Use NuGet to include HtmlAgilityPack(Vs2012)

namespace CatchWebSample
    class Program
        public static void Main(string[] args)
            HtmlDocument document = new HtmlDocument();
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.GetEncoding("utf-8");
            string content = wc.DownloadString("");
            string oldContent = document.DocumentNode.OuterHtml;

            //here, I want to replace all xpath= //div[@class='post_item_foot'] htmlnodes
            HtmlNodeCollection targetNodeCollection = document.DocumentNode.SelectNodes(@"//div[@class='post_item_foot']");

            HtmlNode newHtmlNode;
            if (targetNodeCollection != null && targetNodeCollection.Count > 0)
                for (int i = 0; i < targetNodeCollection.Count; i++)
                    var targetNode = targetNodeCollection[i];
                    newHtmlNode = document.CreateElement("span");
                    newHtmlNode.InnerHtml = HtmlDocument.HtmlEncode("###### REPLACED CONTENT #########");

                    targetNode.ParentNode.ReplaceChild(newHtmlNode, targetNode);

                content = document.DocumentNode.OuterHtml;

                //but the result is same of the original data,why it can not replace ?
                bool flag = string.Compare(oldContent, content) == 0;


I'm so confuse, and why?

4/28/2014 10:08:05 AM

Popular Answer

replaceChild() , insertAfter() suddeny both of them stopped working for me too.

my best alternative was to replacing innerhtml with "new html string"

targetnode.innerhtml = newNodeAsString;


there was a bug at HtmlAgilityPack inserting values/nodes. due to some caching to make it work faster.

thats why i ditched it. and used AngleSharp.


at mid 2017 HAP is being developed here. i won't go back . also you cant create issue at HAP.

4/3/2018 12:49:38 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