---
title: "Extract changes from an HTML diff report in C#"
url: https://recursiveai.org/onediff/cookbook/extract-diff-changes-from-html-report/
description: "Read insertions and deletions from a OneDiff HTML report in C# using its ins, del, and data-change markup. No dedicated report import API is needed."
version: "1.0.1"
---

# Extract changes from an HTML diff report 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/extract-diff-changes-from-html-report/extract-diff-changes-from-html-report.zip)

Read `<ins>` and `<del>` in a OneDiff report. The zip’s `before.txt` and `after.txt` change a fare from $8 to $9.

```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 System.Text.RegularExpressions;
using System.Xml.Linq;
using OneDiff.Documents;

Redline.Create(
    "before.txt", "after.txt", "diff.html");
string html = File.ReadAllText("diff.html");
const string start = "<(ins|del) " +
    "data-change=\"([0-9]+)\">";
var marks = Regex.Matches(html,
    start + @"(.*?)</\1>",
    RegexOptions.Singleline);
foreach (Match mark in marks)
{
    string fragment = mark.Groups[3].Value;
    var body = XElement.Parse(
        $"<text>{fragment}</text>",
        LoadOptions.PreserveWhitespace);
    string text = string.Concat(body.Nodes()
        .OfType<XText>()
        .Select(node => node.Value));
    string kind = mark.Groups[1].Value;
    string id = mark.Groups[2].Value;
    Console.WriteLine($"{id} {kind}: {text}");
}
```

If you paste the code into a new project, create `before.txt` and `after.txt` next to `Program.cs` before running it. Put “The fare is $8 & tax.” in `before.txt` and “The fare is $9 & tax.” in `after.txt`, without the quotation marks.

Both output lines carry change ID `0`: they are the removed and inserted parts of one replacement. The regex looks for `<ins data-change="…">` and `<del data-change="…">` in the HTML that OneDiff 1.0.1 writes. `XElement` reads each matched body, not the entire HTML file. Reading only the text directly inside each mark skips labels and symbols that stand in for special characters or paragraph breaks. The snippet is for plain text edits in OneDiff-generated inline reports, not arbitrary HTML or all change types.

If the report has no matching marks, the loop prints nothing. That alone does not mean the comparison found no differences.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onediff/cookbook/extract-diff-changes-from-html-report/extract-diff-changes-from-html-report.zip) and `dotnet run`.

Writes `diff.html` and prints:

```text
0 del: 8
0 ins: 9
```

## Variations

**Keep machine-readable output when creating the comparison.** Use this in place of the example if you need the full comparison later:

```csharp
using OneDiff.Documents;

var comparison = Comparison.Of(
    "before.txt", "after.txt");
comparison.SaveAs("diff.json");
```

See [how to read changes back from JSON](https://recursiveai.org/onediff/cookbook/export-document-comparison-json/).

## Related recipes

- [Write an inline document redline as HTML](https://recursiveai.org/onediff/cookbook/write-inline-document-redline-html/)
- [Render a PDF redline comparison](https://recursiveai.org/onediff/cookbook/render-text-document-redline-as-pdf/)

## The short answer

Read the visible insertions and deletions in a OneDiff HTML report through its `ins` and `del` tags. `data-change` groups fragments of the same edit. If you already have `diff.html`, skip the call that generates it. OneDiff documents the [report’s markup](https://recursiveai.org/onediff/docs/output-html/#2-inline-layout); the example reads that markup with .NET and does not import it as a `Comparison` object.

## How do you tell which text changed in an HTML diff?

In a OneDiff inline report, `del` holds the old fragment and `ins` holds the new one. A replacement can therefore produce two entries for one change ID. The example prints `8` and `9`, not the words around them: unchanged text sits outside these tags. If you need context, look at the containing paragraph, `p.t`, and its block. The block carries `data-new`, `data-old`, and `data-link` when applicable, which connect it to the compared nodes. Reading the visible text in the browser or stripping all tags is less useful than reading the markup: the labels and glyphs are spans for presentation, not part of the changed wording.

## Can you recover the original files from an HTML diff report?

No. The report describes the comparison as it was made. Visible inline paragraphs show the **compared** old text if you remove insertions and the compared new text if you remove deletions, taking care not to include the presentation labels. That is not a byte-for-byte reconstruction. Under settings that fold differences such as typographic quotes or whitespace, those differences can be counted under “Differences not shown” instead of drawn as edits. Moves, formatting changes, and attribute changes have their own markup and are not included in the loop above. Save JSON when you need the structured comparison, and keep the original files if you need their exact contents.
