Call Import on the destination, then append the copy. The original stays put. To find the destination parent, query the read-only snapshot, use ToMutable to get an editable handle, and call Commit() to make your edits visible in the next snapshot.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml; using RecursiveAI.OneHtml.Mutation; var source = Document.Parse( "<aside id='tip'><b>Save</b>" + " a copy.</aside>"); var dest = MutableDocument.Parse("<body>"); var start = dest.Snapshot(); var body = start.ToMutable( start.Document.Root.QuerySelector("body")); var tip = source.GetElementById("tip"); var copy = dest.Import(tip, deep: true); body.AppendChild(copy); dest.Commit(); Console.WriteLine(dest.SerializeChildren(body));
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The destination gets the complete <aside>, including its nested <b>:
<aside id="tip"><b>Save</b> a copy.</aside>The full program
The full program renames the imported element after inserting it, then prints both versions. The source keeps its original ID.
using RecursiveAI.OneHtml; using RecursiveAI.OneHtml.Mutation; var source = Document.Parse( "<aside id='tip'><b>Save</b>" + " a copy.</aside>"); var dest = MutableDocument.Parse("<body>"); var start = dest.Snapshot(); var body = start.ToMutable( start.Document.Root.QuerySelector("body")); var tip = source.GetElementById("tip"); var copy = dest.Import(tip, deep: true); body.AppendChild(copy); dest.Commit(); Console.WriteLine(dest.SerializeChildren(body)); copy.SetAttribute("id", "copied-tip"); dest.Commit(); Console.WriteLine($"Copy: {dest.SerializeNode(copy)}"); Console.WriteLine($"Original: {source.SerializeNode(tip)}");
<aside id="tip"><b>Save</b> a copy.</aside> Copy: <aside id="copied-tip"><b>Save</b> a copy.</aside> Original: <aside id="tip"><b>Save</b> a copy.</aside>
If you pass a missing node or the document root itself to Import, it throws ArgumentException; select a real element in the source first.
Variations
Copy just the element and its attributes. Add this after the full program when you want the outer tag as a template for new content:
var shell = dest.Import(tip, deep: false); shell.SetAttribute("id", "empty-tip"); body.AppendChild(shell); dest.Commit(); Console.WriteLine(dest.SerializeNode(shell));
<aside id="empty-tip"></aside>Related recipes
The short answer
Find the source element in a parsed Document, then call Import(sourceNode, deep: true) on the destination MutableDocument. This copies the whole subtree: the element, its attributes, and all the nodes below it. The copy starts out detached, so append it to the desired parent, then commit the destination. The original stays put.
What’s the difference between importing and cloning a DOM node?
Both make a copy. The difference is who owns the copy. In onehtml, CloneNode makes a copy from a MutableNode in the same mutable document. Import accepts a Node from an immutable Document and makes new nodes owned by the destination. It’s the same distinction as the browser’s importNode(), but onehtml’s method is named Import and defaults to copying descendants.
Don’t append a live MutableNode from a different mutable document. onehtml rejects that operation with DomException and Kind set to WrongDocument. If the source is also mutable, commit any pending source edits and select the source element from its snapshot’s Document. Pass that immutable node to the destination’s Import.
How do I copy an element without copying its children?
Set deep: false. The variation keeps an <aside> but doesn’t copy the <b> or either text node. The id is copied along with the other attributes; the variation changes it before insertion. If you import a subtree more than once, check the IDs of both the root and its descendants in the destination so that selectors or markup references don’t become ambiguous.
Does importing change the source HTML?
No. The copy has its own identity and may be edited without changing the original. In the full program, the destination gets id="copied-tip", while the source still has id="tip". The change appears in the destination’s next snapshot after the second commit. Import works on the parsed tree, not on the original byte range: for instance, the printed HTML uses double quotes even though the source markup uses single quotes. Use it to preserve the element structure, not the source formatting.