---
title: "Set innerHTML on an HTML element in C#"
url: https://recursiveai.org/onehtml/cookbook/replace-element-inner-html-with-mutable-dom/
description: "Replace an element’s child markup with SetInnerHtml in C#, parse a table row in context, and read the result from a committed onehtml snapshot."
version: "1.7.0"
---

# Set innerHTML on an HTML element 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/replace-element-inner-html-with-mutable-dom/replace-element-inner-html-with-mutable-dom.zip)

Call `SetInnerHtml` to replace the children. Look up the element in 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.Mutation;

var doc = MutableDocument.Parse(
    "<table id='orders'></table>");
var start = doc.Snapshot();
var node = start.Document
    .GetElementById("orders");
var table = start.ToMutable(node);
table.SetInnerHtml("<tr><td>Paid</td></tr>");
doc.Commit();
Console.WriteLine(doc.SerializeChildren(table));
```

[The example](https://recursiveai.org/onehtml/cookbook/replace-element-inner-html-with-mutable-dom/replace-element-inner-html-with-mutable-dom.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 parser adds `<tbody>` around the new row:

```text
<tbody><tr><td>Paid</td></tr></tbody>
```

## The full program

The full program also serializes the table from the new snapshot and from the original parsed document. The original still has no rows.

```csharp
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse(
    "<table id='orders'></table>");
var start = doc.Snapshot();
var node = start.Document
    .GetElementById("orders");
var table = start.ToMutable(node);
table.SetInnerHtml("<tr><td>Paid</td></tr>");
doc.Commit();
Console.WriteLine(doc.SerializeChildren(table));

var after = doc.Snapshot();
Console.WriteLine(after.Document.SerializeNode(after.ToNode(table)));
Console.WriteLine($"Original: {start.Document.SerializeNode(node)}");
```

```text
<tbody><tr><td>Paid</td></tr></tbody>
<table id="orders"><tbody><tr><td>Paid</td></tr></tbody></table>
Original: <table id="orders"></table>
```

If your lookup finds no element, check `node.IsNull` before calling `ToMutable`; `SetInnerHtml` also rejects non-element targets.

## Variations

**Insert another row without replacing the first.** Add this after the full program. Insert into the `<tbody>` created by the first call, not directly into the table:

```csharp
var rows = table.FirstChild; // The generated tbody.
rows.InsertAdjacentHtml(InsertPosition.BeforeEnd,
    "<tr><td>Pending</td></tr>");
doc.Commit();
Console.WriteLine(doc.SerializeChildren(rows));
```

```text
<tr><td>Paid</td></tr><tr><td>Pending</td></tr>
```

## Related recipes

- [Parse HTML fragments in C#](https://recursiveai.org/onehtml/cookbook/html-fragments/)
- [Edit HTML without reformatting it](https://recursiveai.org/onehtml/cookbook/edit-html-csharp-lossless/)

## The short answer

Call `SetInnerHtml(markup)` on a `MutableNode` that represents an element. This is onehtml’s C# equivalent of setting an element’s [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML): the string is parsed as HTML and replaces the children, not the enclosing tag or its attributes. Call `Commit()` after your changes to make them available in a new snapshot.

## Why does setting innerHTML on a table add a tbody?

`SetInnerHtml` parses a fragment in the context of its target, as described in the [mutable DOM docs](https://recursiveai.org/onehtml/docs/mutable-document/#rules-the-mutation-methods-follow). The context includes the target’s tag name and namespace. In the example, a bare `<tr>` becomes a row inside an implied `<tbody>` because the target is a `<table>`. Don’t expect the child markup you read back to match the input string byte for byte.

If the target is already a `<tbody>`, pass row markup without another `<tbody>` wrapper. To replace just the content of one cell, target that `<td>` instead. The choice of target determines both what the parser builds and which old children it removes.

## Should I use textContent or innerHTML?

Use `SetTextContent` to replace the children with text. Use `SetInnerHtml` when you intend the string’s tags to become elements. The difference matters even for short strings: the following code, added after the full program, puts the literal characters `<b>Paid</b>` in the cell. It does not make the word bold.

```csharp
var cell = table.FirstChild.FirstChild.FirstChild;
cell.SetTextContent("<b>Paid</b>");
doc.Commit();
Console.WriteLine(doc.SerializeChildren(cell));
```

```text
&lt;b&gt;Paid&lt;/b&gt;
```

Serialization escapes the angle brackets so the text stays text when the markup is read again. If the input is meant to be displayed as text, use the text method rather than manually inserting escapes in a string of markup.

## How do I get an element’s inner HTML after changing it?

Use `doc.SerializeChildren(table)` to read the markup inside the element; `doc.SerializeNode(table)` includes the element’s own tags. These methods read the live tree, so you can inspect the result even before committing. If another API takes an immutable `Document`, commit first, get `doc.Snapshot()`, and map the element into that snapshot with `ToNode`, as the full program does. The original parsed document won’t pick up the edit later.
