---
title: "Compare document version history in C#"
url: https://recursiveai.org/onediff/cookbook/compare-document-version-history/
description: "Compare sequential document versions, keep each step’s edits, and write a report of the net change from first to last."
version: "1.0.1"
---

# Compare document version history 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-document-version-history/compare-document-version-history.zip)

Compare sequential document versions, keep each step’s edits, and write a report of the net change from first to last.

This release accepts text files, digital PDFs with the PDF package, and scans with the OCR package, but not Word documents; see the [file-input example](#chain-files-from-disk).

```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.Combine;
using OneDiff.Documents.Model;

var chain = ComparisonChain.Of([
    CompareDocument.FromText("Pay in 30 days."),
    CompareDocument.FromText("Pay in 60 days."),
    CompareDocument.FromText("Pay in 90 days.")]);
chain.SaveAs("history.html");
Console.WriteLine(chain.Steps.Length);
```

Writes `history.html`.

```text
2
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/compare-document-version-history/compare-document-version-history.zip), then `dotnet run`.

## Full program

All inputs are inline: v1 says “Pay in 30 days.”, v2 says 60 days and v3 says 90 days. The full program names the versions and writes `history.html`. Step indices printed in attribution are zero-based. A chain with fewer than two versions throws `ArgumentException`.

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

var chain = ComparisonChain.Of([
    CompareDocument.FromText("Pay in 30 days.", "v1"),
    CompareDocument.FromText("Pay in 60 days.", "v2"),
    CompareDocument.FromText("Pay in 90 days.", "v3")]);
chain.SaveAs("history.html");
foreach (var step in chain.Steps)
    Console.WriteLine($"{step.FromName} -> {step.ToName}: {step.Comparison.Changes.Length} change(s)");
Console.WriteLine($"Net changes: {chain.Net.Changes.Length}");
foreach (var change in chain.Net.Changes)
    Console.WriteLine($"Net change {change.Id}: steps {string.Join(", ", chain.StepsOf(change.Id))}");
Console.WriteLine($"Check failures: {chain.Check().Length}");
```

```text
v1 -> v2: 1 change(s)
v2 -> v3: 1 change(s)
Net changes: 1
Net change 0: steps 0, 1
Check failures: 0
```

## Variations

**Keep the complete chain as JSON.** The output extension picks the report format: `.html` writes HTML and `.json` writes JSON.

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

var chain = ComparisonChain.Of([
    CompareDocument.FromText("Pay in 30 days.", "v1"),
    CompareDocument.FromText("Pay in 60 days.", "v2"),
    CompareDocument.FromText("Pay in 90 days.", "v3")]);
chain.SaveAs("history.json");
Console.WriteLine(chain.Net.Verification.Passed);
```

```text
True
```

### Chain files from disk

The zip includes `v1.txt`, `v2.txt` and `v3.txt`, containing “Pay in 30 days.”, “Pay in 60 days.” and “Pay in 90 days.” respectively, each followed by a newline. Plain text needs no extra package or registration.

```csharp
using OneDiff.Documents;

Redline.Chain(["v1.txt", "v2.txt", "v3.txt"], "files.html");
Console.WriteLine("Wrote files.html");
```

```text
Wrote files.html
```

## Related recipes

- [Compare scanned documents](https://recursiveai.org/onediff/cookbook/compare-scanned-documents/)
- [Export a document comparison to JSON](https://recursiveai.org/onediff/cookbook/export-document-comparison-json/)
- [Combine reviewer changes](https://recursiveai.org/onediff/cookbook/combine-reviewer-changes/)

## The short answer

Use `ComparisonChain.Of` from `RecursiveAI.OneDiff.Documents` with document models (in-memory content and structure) ordered oldest to newest. It compares each adjacent pair and compares the first version directly with the last. Save the chain as HTML for a report or JSON for complete comparisons and attribution. The chain does not retrieve a repository’s history: supply the versions you want to compare.

This example deliberately changes the same payment term twice. The step view shows 30 to 60 days, then 60 to 90 days. The net view shows 30 to 90 days.

## How to compare two versions of a document?

For two versions, an ordinary comparison is enough. For a sequence, put the models into chronological order before calling `ComparisonChain.Of`. The library uses the order supplied; a name such as “v3” does not cause sorting. The example uses inline text so the full input is visible and no document reader (a file-to-model adapter) needs registering.

Every step is an ordinary comparison with its own changes and completeness result. The chain reuses each intermediate document object: a step’s new document is the next step’s old document. The net comparison connects the first model to the last. `chain.Check()` checks the comparisons and confirms those connections, returning failures without changing the immutable comparison objects.

The full program prints one change for each step, one net change and no check failures. Those are results for the sample payment sentence, not an expected count for arbitrary version histories. A later version can reverse an earlier edit, leaving a change in the step history but no corresponding final difference.

## How to view version history in a document?

Open the generated `history.html`. The report includes a section for each step followed by a net section. Each net change lists the steps that touched it. The [version-chain documentation](https://recursiveai.org/onediff/docs/combine/) defines this attribution at block granularity, not at the granularity of a single final character.

If two steps edit different words in one paragraph, both steps can be attributed to both net changes in that paragraph. Consult the individual step comparison to establish exactly what each step did. Do not turn a net attribution list into a claim that every listed step authored every word of the final edit.

Names supplied to `CompareDocument.FromText` make report headings readable. Choose names that distinguish versions, and retain your application’s own timestamps and identifiers separately if you need them. OneDiff compares supplied content; it does not infer who edited it or discover earlier versions hidden in an external document service.

## How do I compare document version history in a C# example?

Run the three-version download, then replace its inline strings with your own ordered text. To archive the comparison, use the JSON variation. Its chain form contains version names and hashes, complete step comparisons, a net comparison and change-to-step attribution. It is not just the HTML report encoded as a string.

Sequential history is different from independent reviewer copies. In a chain, each input follows the previous input. In reviewer combination, all copies are compared with one original and disagreements are preserved as conflicts. Use [reviewer combination](https://recursiveai.org/onediff/cookbook/combine-reviewer-changes/) for that branching workflow, not a chain ordered arbitrarily by reviewer name.

This published release does not accept Word documents. For supported file inputs, load them through the appropriate registered reader or use the path-based `Redline.Chain` entry point described in the documentation. The inline text example does not create tracked revisions inside an Office file.
