---
title: "Observe HTML DOM mutations in C#"
url: https://recursiveai.org/onehtml/cookbook/observe-mutable-dom-commits/
description: "Observe HTML DOM mutations in C# with onehtml. Subscribe to commits and read inserted nodes, attribute changes, and more without polling the document."
version: "1.7.0"
---

# Observe HTML DOM mutations 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/observe-mutable-dom-commits/observe-mutable-dom-commits.zip)

Use `Observe` to hear about edits when you call `Commit()` on a parsed HTML tree.

```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("<p>Hi");
using var watch = doc.Observe(c =>
    Console.WriteLine(
        c.Changes.Attributes.Count));
var root = doc.DocumentElement;
root.SetAttribute("lang", "en");
doc.Commit();
```

[The example](https://recursiveai.org/onehtml/cookbook/observe-mutable-dom-commits/observe-mutable-dom-commits.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 callback prints the number of elements whose attributes changed:

```text
1
```

## The full program

Start with an empty `<ul>`, append an item, and read the inserted subtree from the commit. After the `using` block ends, another commit still succeeds but the callback no longer runs.

```csharp
using RecursiveAI.OneHtml.Mutation;

var doc = MutableDocument.Parse("<ul></ul>");
// html → body → ul
var list = doc.DocumentElement.LastChild.FirstChild;

using (doc.Observe(commit =>
{
    Console.WriteLine($"Commit {commit.Version}");
    foreach (var insertion in commit.Changes.Inserted)
        Console.WriteLine($"Added: {insertion.Node.TextContent}");
}))
{
    var item = doc.CreateElement("li");
    item.SetTextContent("Pack lunch");
    list.AppendChild(item);
    doc.Commit();
}

list.AppendChild(doc.CreateElement("li"));
var later = doc.Commit();
Console.WriteLine($"Committed after unsubscribe: {later.Version}");
```

`dotnet run` prints:

```text
Commit 1
Added: Pack lunch
Committed after unsubscribe: 2
```

If your callback never runs, check that you registered it before the edit and called `Commit()` while the registration was still active. Calling `Commit()` with no pending edits doesn’t notify it.

## Variations

**Run only when the list gains a child.** Replace the `using` block header in the full program (keep its body). `Observe` subscribes to the whole document, so filter the reported insertions by parent:

```csharp
using (doc.Observe(commit =>
{
    foreach (var insertion in commit.Changes.Inserted)
        if (insertion.Parent == list)
            Console.WriteLine(insertion.Node.LocalName);
}))
```

The filtered callback prints `li`, followed by the program’s `Committed after unsubscribe: 2` line.

## Related recipes

- [Get changes since an HTML DOM snapshot in C#](https://recursiveai.org/onehtml/cookbook/snapshot-and-diff-mutable-dom/)
- [Toggle a CSS class on an HTML element in C#](https://recursiveai.org/onehtml/cookbook/toggle-html-class-in-mutable-dom/)

## The short answer

Register a callback with `MutableDocument.Observe`, make edits, then call `Commit()`. Your callback reads `commit.Changes`, which groups the changes by kind rather than recording every method call. Keep the returned `IDisposable` for as long as you want notifications. Disposing it stops future delivery to that callback; it doesn’t stop the document from accepting edits.

## Is this the same as a browser MutationObserver?

Both let you react to changes instead of repeatedly reading the tree. A browser’s [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) watches nodes in the browser and calls back with mutation records that match its options. onehtml’s `Observe` watches one `MutableDocument` in your C# process. It won’t report changes to a page open in a browser or edits made to the source file on disk.

onehtml runs the callbacks on the thread that calls `Commit()`, in registration order. You can read the supplied change set there without building a snapshot. If a callback commits more edits, onehtml queues that commit to deliver after the current one. A callback that repeatedly edits the document it watches can therefore trigger more notifications; check what changed before making another edit.

## What DOM mutations can you observe?

Read `Inserted`, `Removed`, and `Moved` for structural changes; `Attributes` or `Classes` for changes to existing elements; and `Text` for changes to character data, such as editing an existing text node. When you insert a whole subtree, `Inserted` names its root, not every descendant. In the list example the text is already part of the new `<li>`, so the callback reads it from that node instead of looking for a separate text record.

The changes describe the result of a batch. If you give an existing element a temporary attribute value and replace it before committing, the observer sees the original and final values, not the temporary one. A commit can even notify you with an empty change list when there were pending edits but nothing is listed. Use `commit.Changes.IsEmpty` to tell whether there is anything in the change set to process. The [mutable document reference](https://recursiveai.org/onehtml/docs/mutable-document/) lists the exact rules for combining edits.
