---
title: "Copy an HTML subtree to another document in C#"
url: https://recursiveai.org/onehtml/cookbook/clone-html-subtree-between-documents/
description: "Import an HTML element and its descendants into another document in C#. Attach the copy to a new parent without changing the original."
version: "1.7.0"
---

# Copy an HTML subtree to another document in C#

**onehtml** is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/clone-html-subtree-between-documents/clone-html-subtree-between-documents.zip)

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.

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onehtml/cookbook/clone-html-subtree-between-documents/clone-html-subtree-between-documents.zip) 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>`:

```text
<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.

```csharp
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)}");
```

```text
<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:

```csharp
var shell = dest.Import(tip, deep: false);
shell.SetAttribute("id", "empty-tip");
body.AppendChild(shell);
dest.Commit();
Console.WriteLine(dest.SerializeNode(shell));
```

```text
<aside id="empty-tip"></aside>
```

## Related recipes

- [Move a DOM node without losing its identity](https://recursiveai.org/onehtml/cookbook/move-dom-node-without-losing-identity/)
- [Parse an HTML document in C#](https://recursiveai.org/onehtml/cookbook/parse-html/)

## 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()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/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.
