2

我有这个

身体:

<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>
4

1 回答 1

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); 
于 2010-04-08T20:40:42.830 回答