HtmlAgilityPack - Replace Node With Another Node

asp.net c# html-agility-pack vb.net

Question

I have two nodes that have inner HTML that goes something like this:

Node1:

<p>some text</p>
<p>some more text</p>

Node2:

<p>some text</p>
<p>some more text</p>
<div><p>lots of more paragraphs here</p></div>

I'm trying to look up Node1 in Node2 in order to add <!--more--> tag (for WordPress posts) following the first two paragraphs (or whatever HTML text is supplied in Node1).

Both of the above nodes have different parents. How do I find and replace or otherwise append the <!--more--> tag in such case? I tried running the following code, but it errors out:

Code:

node2.ParentNode.ReplaceChild(HtmlNode.CreateNode(node1.InnerHtml & "<!--more-->"), node1).InnerHtml

Error:

Node "

             " was not found in the collection
Parameter name: node

Also tried chaging node1 in the oldChild part to HtmlNode.CreateNode(node1.InnerHtml) but that didn't work either.

Accepted Answer

I think it would be easier to perform a replacement on HtmlNode.InnerHtml property directly:

node2.InnerHtml = node2.InnerHtml.Replace(
    node1.InnerHtml, 
    node1.InnerHtml + "<!--more-->"
);

Popular Answer

Not sure why replacing doesn't work, however there's an InsertAfter Method you can use.

const string html = @"<p>some text</p>
<p>some more text</p>
<div><p>lots of more paragraphs here</p></div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);
var secondParagraph = doc.DocumentNode.SelectSingleNode("//p[2]");
// var secondParagraph = doc.DocumentNode.Descendants("p").Skip(1).First(); //or Linq
var moreNode= HtmlNode.CreateNode("<!--more-->");
doc.DocumentNode.InsertAfter(moreNode,secondParagraph );


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