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.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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:
<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.
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)}");
<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:
var rows = table.FirstChild; // The generated tbody. rows.InsertAdjacentHtml(InsertPosition.BeforeEnd, "<tr><td>Pending</td></tr>"); doc.Commit(); Console.WriteLine(doc.SerializeChildren(rows));
<tr><td>Paid</td></tr><tr><td>Pending</td></tr>Related recipes
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: 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. 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.
var cell = table.FirstChild.FirstChild.FirstChild; cell.SetTextContent("<b>Paid</b>"); doc.Commit(); Console.WriteLine(doc.SerializeChildren(cell));
<b>Paid</b>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.