---
title: "Map a DOMSnapshot to HTML in C#"
url: https://recursiveai.org/onehtml/cookbook/map-browser-layout-snapshot-to-dom/
description: "Map captured layout boxes to HTML nodes; OneHtml does not launch Chrome."
version: "1.6.0"
---

# Map a DOMSnapshot to HTML 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.Accessibility) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/map-browser-layout-snapshot-to-dom/map-browser-layout-snapshot-to-dom.zip)

Map captured layout boxes to HTML nodes; OneHtml does not launch Chrome.

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

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

`page.html` and `snapshot.json` are the paired capture files in the zip.

```csharp
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Accessibility.Rendered;

var doc = Document.Parse(File.ReadAllText("page.html"));
var snapshot = DomSnapshot.Parse(
    File.ReadAllBytes("snapshot.json"));
var result = DomSnapshotAdapter.Map(snapshot, doc);
if (!result.Succeeded)
    throw new InvalidOperationException(result.Refusal!.Message);
var visible = doc.GetElementById("visible");
Console.WriteLine($"Visible: {result.Facts![visible].HasLayout}");
```

```text
Visible: True
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/map-browser-layout-snapshot-to-dom/map-browser-layout-snapshot-to-dom.zip), then `dotnet run`.

## Full program

```csharp
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Accessibility.Rendered;

var doc = Document.Parse(File.ReadAllText("page.html"));
var snapshot = DomSnapshot.Parse(
    File.ReadAllBytes("snapshot.json"));
var result = DomSnapshotAdapter.Map(snapshot, doc);
if (!result.Succeeded)
    throw new InvalidOperationException(result.Refusal!.Message);
var visible = doc.GetElementById("visible");
Console.WriteLine($"Visible: {result.Facts![visible].HasLayout}");
Console.WriteLine($"Hidden: {result.Facts![doc.GetElementById("none")].HasLayout}");
Console.WriteLine($"Width: {result.Facts![visible].Layout.BorderBox}");
var different = Document.Parse("<main>Different page</main>");
var refused = DomSnapshotAdapter.Map(snapshot, different);
Console.WriteLine($"Mismatch: {refused.Refusal!.Reason}");
Console.WriteLine($"Facts returned: {refused.Facts is not null}");
```

```text
Visible: True
Hidden: False
Width: LayoutRect { X = 8, Y = 87.671875, Width = 1264, Height = 22.390625, Right = 1272, Bottom = 110.0625, IsEmpty = False }
Mismatch: NodeCountMismatch
Facts returned: False
```

The zip includes the paired `page.html` and `snapshot.json` from the public OneHtml hidden-content capture, with its MIT license. The full program also maps a different document: a mismatch returns a refusal and no facts; malformed snapshot JSON throws `DomSnapshotFormatException`.

The companion DOM includes this excerpt; the full HTML and captured layout tables are in the zip:

```html
<p id="visible">This paragraph is visible.</p>
<p id="none" class="gone">This paragraph is display:none.</p>
```

Its style sheet sets `.gone { display: none; }`.

## Variations

**Inspect an attribute mismatch.** After loading the snapshot, change an ID in its companion markup:

```csharp
var mismatch = Document.Parse(
    File.ReadAllText("page.html").Replace("id=\"visible\"", "id=\"changed\""));
var values = DomSnapshotAdapter.Map(snapshot, mismatch);
Console.WriteLine($"Attributes: {values.Refusal!.Reason}");
```

```text
Attributes: AttributeMismatch
```

## Related recipes

[Get computed CSS styles](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/) · [Inspect the accessibility tree](https://recursiveai.org/onehtml/cookbook/accessibility-tree/)

---

## The short answer

Read the captured JSON with `DomSnapshot.Parse`, parse the corresponding serialized DOM with `Document.Parse`, then pass both to `DomSnapshotAdapter.Map`. Check `Succeeded` before using `Facts`. Query the facts with nodes from that exact document, not nodes obtained from another parse of identical HTML.

The [rendered-facts documentation](https://recursiveai.org/onehtml/docs/rendered-facts/) describes a strict mapping. It is designed to refuse a mismatch instead of attaching a browser box to the wrong element. The full program shows both acceptance and refusal using the same capture.

## What is the Chrome debugging protocol?

The [Chrome DevTools Protocol DOMSnapshot domain](https://chromedevtools.github.io/devtools-protocol/tot/DOMSnapshot/) exposes `captureSnapshot`, which returns flattened document, node and layout tables. OneHtml consumes those tables; it does not connect to Chrome or run page script.

Capture the serialized DOM in the same session as the snapshot, with page script disabled. The intended markup is `document.documentElement.outerHTML`, optionally preceded by the serialized doctype. Original HTML bytes are suitable only when script has not changed the DOM. Otherwise the parser and the snapshot may describe different trees.

A bare protocol result contains `documents` and `strings`. The reader also accepts a protocol response wrapping that result, or the repository’s capture-file wrapper. The example uses the wrapper so the requested computed-style names travel with the snapshot. This matters because a bare result does not say which style properties were requested.

## How do I convert DOMSnapshot.captureSnapshot output to HTML?

For this task, do not try to reconstruct HTML from the layout table. Capture the serialized DOM separately and parse it. The adapter pairs the two structural representations and gives the parsed nodes browser-derived facts. The sample’s `page.html` is that companion DOM, not HTML generated from its rectangles.

The mapper compares structure, text and, by default, attribute values. A different element count, changed text or changed attributes can refuse the mapping. The refusal contains a reason and diagnostic location; `Facts` is absent. Malformed JSON and inconsistent snapshot tables instead throw `DomSnapshotFormatException` during reading.

The strict attribute-value check is intentional: changing a class or inline style can change the box that the snapshot measured. Do not relax that check just to make a stale capture pass. The variation demonstrates an attribute mismatch without silently accepting it.

## What does a DOMSnapshot capture tell me about layout?

A mapped node’s `HasLayout` tells you whether it has a box. `Layout.BorderBox` is in document coordinates, in CSS pixels. `display: none` nodes have no box; `visibility: hidden` and zero-opacity nodes can still have boxes. Consequently, a layout box is not evidence that someone can see or interact with the content.

Request `includePaintOrder`, `includeDOMRects`, `includeBlendedBackgroundColors` and `includeTextColorOpacities` when those facts are needed. These tables are optional: absence stays absence. Specify the computed-style names at capture time and pass that same ordered list to the parser for a bare result. A fact cannot recover a style or state that was never captured.

Pseudo-elements become generated boxes on their hosts. Shadow-tree nodes have no corresponding parsed light-DOM nodes; the host keeps its own facts. Frame documents are separate snapshot documents. The adapter accounts for these categories rather than assuming every captured node maps to an ordinary element. Re-capture after changing the page, viewport or browser version; the snapshot is evidence of that captured state, not a live layout service.
