Reputation: 2817
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.
Upvotes: 0
Views: 8610
Reputation: 5197
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 );
Upvotes: 0
Reputation: 32333
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-->"
);
Upvotes: 3