我有这个
身体:
<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);
}
执行此操作后,尽管OutterHtml或InnerText显示出良好的结果,但身体节点的InnerHtml仍然与开头相同。我的代码有问题吗?
结果:
// 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>
答案 0 :(得分:0)
我认为这可能与添加节点以替换旧节点的方式有关。查看此解决方案是否适用于截断文本节点。我做了一个快速测试,所有三个给了我相同的结果。
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
HtmlNode body = doc.DocumentNode.SelectSingleNode("//body");
foreach (var paragraph in body.Descendants("p"))
{
paragraph.InnerHtml = paragraph.InnerHtml.Substring(0, 25) + "...";
}
Console.WriteLine(body.InnerHtml);
Console.WriteLine(body.InnerText);
Console.WriteLine(body.OuterHtml);