Commit() applies your edits to the document; Flush() updates the live page’s layout so your app can repaint it.
dotnet add package RecursiveAI.OneRender.ImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
RecursiveAI.OneHtml, which provides RecursiveAI.OneHtml.Mutation, comes with RecursiveAI.OneRender.Image.
using OneRender; using OneRender.Image; using OneRender.Interaction; using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse(""" <ul id="list"><li>Draft</li></ul> """); var page = InteractivePage.Create(doc, new HtmlPageOptions( FontProviders.CreateDefault())); var list = page.MutableNodeOf( page.FindElement("list")); list.FirstChild.Remove(); var item = doc.CreateElement("li"); var label = doc.CreateTextNode("Published"); item.AppendChild(label); list.AppendChild(item); doc.Commit(); page.Flush(); var published = page.NodeOf(item); var text = page.GetTextContent(published); Console.WriteLine(text);
Replaces “Draft” with “Published” on the existing rendered page.
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.
PublishedThe full program
using OneRender; using OneRender.Image; using OneRender.Interaction; using OneRender.Painting; using RecursiveAI.OneHtml.Mutation; var doc = MutableDocument.Parse(""" <style>li { font: 20px Arial; color: #153b5b }</style> <ul id="list"><li>Draft</li></ul> """); var page = InteractivePage.Create(doc, new HtmlPageOptions(FontProviders.CreateDefault()) { ViewportWidth = 240, ViewportHeight = 64 }); var list = page.MutableNodeOf(page.FindElement("list")); var draft = list.FirstChild; Save("before.png"); draft.Remove(); var item = doc.CreateElement("li"); item.AppendChild(doc.CreateTextNode("Published")); list.AppendChild(item); doc.Commit(); page.Flush(); Console.WriteLine($"Draft attached: {!page.NodeOf(draft).IsNull}"); Console.WriteLine(page.GetTextContent(page.NodeOf(item))); Save("after.png"); void Save(string path) { var drawing = new DisplayList(); page.Page.Paint(drawing); var image = DisplayListRasterizer.Render(drawing, 240, 64); File.WriteAllBytes(path, image.ToPng()); Console.WriteLine($"Wrote {path}"); }
Wrote before.png Draft attached: False Published Wrote after.png
Open before.png and after.png. The bullet reads “Draft” in the first image and “Published” in the second. Both images come from the same InteractivePage.
A removed element has no current page node: page.NodeOf(draft).IsNull is true. Keep the MutableNode for later edits, not a snapshot Node from before the flush.
Variations
Insert before another element. To keep “Draft” and put “Published” above it, skip list.FirstChild.Remove() and replace the AppendChild call with:
list.InsertBefore(item, list.FirstChild);Related recipes
- Change HTML text, classes and attributes in C#
- Resize a live HTML viewport and set device pixel ratio in C#
The short answer
Parse the HTML into a MutableDocument and create the InteractivePage from that document, not from the source string. Add or remove children on the mutable tree, call doc.Commit() to publish the edits, then page.Flush() to update the rendered layout. Paint the current page to make the change visible in your host. A PNG you saved before the flush is still just a snapshot; repaint and save again to see the new list.
How do I add and remove HTML elements dynamically in C#?
Use CreateElement to create an element, CreateTextNode for its text, and AppendChild to attach it under an existing parent. Creating a node alone doesn’t put it on the page. To remove an item, call Remove() on that item, not on its parent. To insert in the middle of a list, use InsertBefore on the parent with the existing item as the reference. In the program, the starting list has one child, so FirstChild is the element to remove. In a list with line breaks and indentation, that first child could instead be a text node; use a specific element handle when the structure varies.
You can group the remove and insert into one commit, as the example does. If your app is taking a reading between two edits, be aware that a layout query such as GetBoundingClientRect on a mutable node commits pending edits before answering. A flush takes the net changes since the last rendered snapshot, so you don’t need a separate repaint for every edit.
Does removing an HTML element reset the rest of the page?
No. While snapshots can renumber nodes, surviving elements retain their listeners, focus, scroll positions and form state. If you remove the focused element itself, it loses focus. Hold on to MutableNode handles and use page.NodeOf when a page API needs a current snapshot Node, such as when setting a form value. page.MutableNodeOf does the reverse, as when the example looks up the list by ID and then edits it. The mutable document guide explains which calls flush automatically and how to release state for nodes you won’t reinsert.