我添加了一个功能来请求“http://cnblogs.com”页面。当我使用HtmlAgilityPack替换更多的HtmlNode时,会遇到一些混淆的问题——无法替换。
代码如下:
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;
}
}
}
}
我很困惑,为什么呢?