Free Support Forum -

Copy Content between two nodes


I am trying to copy content between two nodes like you do it for MailMergeWithRegion() and TableStart/TableEnd.

Is there any API support for that? Or at least what would be the best way to do it?



It could be easy or not so easy depending on your start and end nodes between which you want to copy.

If your start and end nodes are on the same "level" in the tree, for example two paragraphs inside one section, then it is very easy, I will show below.

However, if your start and end nodes form a tree fragment that crosses one or more parent nodes, for example starting node is a paragraph in one section and ending node is a paragraph in another section it will be harder.

Yet when your start and end nodes form a "jagged" tree fragment, for example start node is a paragraph inside a header and end node is a paragraph inside a comment, then a meaningful reproduction of nodes between them will probably be impossible at all.

That's why mail merge with regions only works when the start and end nodes are paragraphs inside one section or paragraphs inside cells of one table row.

Basically, you need to loop from the start node to the end node. The easiest way to do this is using the NextSibling property:

Debug.Assert(startNode.Parent == endNode.Parent);

for (Node node = startNode; node != endNode; node = node.NextSibling)


// This will repeat for all nodes between start and end, given they have the same parent node only!


Inside that loop you basically need to clone the current node and insert into another place in the document where you want it.

Node newNode = srcNode.Clone(true) as Node;

//Add the new node to the destination model just before some other node.

mMark.ParentNode.InsertBefore(newNode, mMark);