---
title: "Compare table cell changes in C#"
url: https://recursiveai.org/onediff/cookbook/compare-structured-table-cells/
description: "Compare two in-memory document tables and report the changed cell’s path and text. Includes a runnable .NET 10 example."
version: "1.0.1"
---

# Compare table cell changes in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff.Documents) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/compare-structured-table-cells/compare-structured-table-cells.zip)

This example builds both tables in C#. It does not open DOCX or spreadsheets.  
For digital PDFs, use [`RecursiveAI.OneDiff.Pdf` 1.0.1](https://recursiveai.org/onediff/docs/pdf/). For DOCX, build a `CompareDocument` with your own reader; no released OneDiff package reads DOCX.

```shell
dotnet add package RecursiveAI.OneDiff.Documents
```

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

```csharp
using OneDiff.Documents;
using OneDiff.Documents.Model;

var result = Comparison.Of(
    Table("Pending"), Table("Approved"));
foreach (var edit in result.Changes)
{
    var path = edit.New.Anchor.Reference;
    Console.WriteLine(path);
    Console.WriteLine(
        $"{edit.Old.Text} → " +
        edit.New.Text);
}

static CompareDocument Table(string status)
{
    var doc = new CompareDocumentBuilder();
    doc.Begin(NodeKind.Table);
    doc.Begin(NodeKind.TableRow);
    doc.Begin(NodeKind.TableCell);
    doc.Add(NodeKind.Paragraph, new()
    {
        Text = status,
        Anchor = Anchor.ForNode(
            "orders/42/status")
    });
    doc.End(); // cell
    doc.End(); // row
    doc.End(); // table
    return doc.Build();
}
```

`dotnet run` prints:

```text
orders/42/status
Pending → Approved
```

`Begin` opens a container; `End` closes it. An anchor is a label you choose for each cell’s location, such as `orders/42/status`. Put it on the paragraph that holds the cell’s text. For an inserted cell, `Old.IsPresent` is `false` and `Old.Text` is empty; for a deleted cell, `New.IsPresent` is `false` and `New.Text` is empty. To read the path from the side that exists, replace the `path` assignment in the loop with:

```csharp
var location = edit.New.IsPresent
    ? edit.New : edit.Old;
var path = location.Anchor.Reference;
```

An inserted or deleted cell can produce separate changes for the cell container and its text. The container has no path in this example; the text change carries the anchor.

Unzip [the example](https://recursiveai.org/onediff/cookbook/compare-structured-table-cells/compare-structured-table-cells.zip), then `dotnet run`.

## Variations

To see where a change came from as well as where it landed, replace `Console.WriteLine(path);` in the loop with the following lines, then remove the now-unused `path` variable:

```csharp
Console.WriteLine(edit.Old.Anchor.Reference);
Console.WriteLine(edit.New.Anchor.Reference);
```

Both lines print `orders/42/status` here because the cell kept its path.

## Related recipes

- [Detect numeric changes in documents](https://recursiveai.org/onediff/cookbook/detect-semantic-value-changes/)
- [Detect moved document paragraphs](https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/)

<!-- article -->

## The short answer

Build each version as a `CompareDocument` with table, row, and cell nodes. Put the cell text in a child paragraph and give that paragraph a source path using `Anchor.ForNode`. After `Comparison.Of`, read the change’s old and new anchors and text. Here, `orders/42/status` identifies order 42’s status cell, which changed from `Pending` to `Approved`.

## How do you compare two tables and identify differences?

Keep the table hierarchy rather than comparing a flat list of lines. A table groups rows, a row groups cells, and a cell can group content such as paragraphs. The container nodes in the example have no text of their own. Only the paragraph contains the word that changes.

The [alignment rules](https://recursiveai.org/onediff/docs/alignment/#4-containers) use matched content to pair containers. They can also pair containers in order when equal numbers of unmatched containers of the same kind fall between matching siblings. Within a paired cell, a single unmatched text block of the same kind on each side can then be paired even when its wording is completely different. That’s why `Pending` and `Approved` show up together rather than as one deletion and one insertion in the sample.

### Which path belongs in a change report?

Use each side’s own path. A node number is its position in that version’s document tree, so inserting a row can change numbers downstream. A path you generate from a persistent row identifier and a column name can stay useful even when rows move. If the source only has row and column positions, record the positions separately for each version rather than treating them as permanent identities.

A source path tells the reader where a change occurred; it does not tell OneDiff which rows to match. The [compare model](https://recursiveai.org/onediff/docs/compare-model/#anchors) describes an anchor as “a source location the engine copies but never interprets.” If you need a strict join by order number, do that pairing in your application instead of expecting the path string to act as a join key.

This is a comparison of document content you supply, not a workbook comparison. The 1.0 packages do not read grids, and the sample does not infer formulas or cell addresses from display text.
