HTML Agility Pack - ReplaceNode 不会更改 Body 的 InnerHTML
我有这个
正文: 代码
<body><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent leo leo, ultrices eu venenatis et, rutrum fringilla dolor.</p></body>
:
HtmlNode body = doc.DocumentNode.SelectSingleNode("//body");
Dictionary<HtmlNode, HtmlNode> toReplace = new Dictionary<HtmlNode, HtmlNode>();
// I do some logic here adding nodes to the toReplace dictionary.
foreach (HtmlNode replaceNode in toReplace.Keys)
{
replaceNode.ParentNod.ReplaceChild(toReplace[replaceNode], replaceNode);
}
在我执行此操作后,正文节点的 InnerHtml 与开始时保持相同,尽管 OutterHtml 或 InnerText 显示了良好的结果。我的代码有问题吗?
结果:
// body.InnerHtml
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent leo leo, ultrices eu venenatis et, rutrum fringilla dolor.</p>
// body.OutterHtml
<body><p>Lorem ipsum dolor sit amet...</p></body>
I have this
The body:
<body><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent leo leo, ultrices eu venenatis et, rutrum fringilla dolor.</p></body>
The code:
HtmlNode body = doc.DocumentNode.SelectSingleNode("//body");
Dictionary<HtmlNode, HtmlNode> toReplace = new Dictionary<HtmlNode, HtmlNode>();
// I do some logic here adding nodes to the toReplace dictionary.
foreach (HtmlNode replaceNode in toReplace.Keys)
{
replaceNode.ParentNod.ReplaceChild(toReplace[replaceNode], replaceNode);
}
After i do this, the InnerHtml of the body node remains the same as from beginning, although the OutterHtml or the InnerText are showing the good result. Is there something wrong with my code?
The result:
// body.InnerHtml
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent leo leo, ultrices eu venenatis et, rutrum fringilla dolor.</p>
// body.OutterHtml
<body><p>Lorem ipsum dolor sit amet...</p></body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能与您添加节点来替换旧节点的方式有关。看看这个解决方案是否适合您截断文本节点。我做了一个快速测试,所有三个都给了我相同的结果。
I think it may be something to do with the way you are adding nodes to replace old nodes. See if this solution works for you to truncate the text node. I did a quick test and all three gave me the same results.