HTML Agility Pack - ReplaceNode doesn't change the InnerHTML of the Body

html-agility-pack innerhtml replace

Question

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>

Accepted Answer

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.

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); 



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why