Reputation: 43
I implement a feature to request "http://cnblogs.com" 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("http://cnblogs.com");
document.LoadHtml(content);
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?
Upvotes: 3
Views: 1546
Reputation: 2817
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;
edit:
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.
edit:
at mid 2017 HAP is being developed here. i won't go back . also you cant create issue at HAP.
Upvotes: 2