---
title: "Write an inline document redline as HTML in C#"
url: https://recursiveai.org/onediff/cookbook/write-inline-document-redline-html/
description: "Write a self-contained inline HTML redline in C#. Show insertions and deletions in context and match their data-change attributes to comparison IDs."
version: "1.0.1"
---

# Write an inline document redline as HTML 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/write-inline-document-redline-html/write-inline-document-redline-html.zip)

Show deletions and insertions in the same paragraph. Try `before.txt` and `after.txt` from the zip.

```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;

var comparison = Comparison.Of(
    "before.txt", "after.txt");
comparison.SaveAs("redline.html",
    RedlineForm.Html);
Console.WriteLine("Saved redline.html");
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/write-inline-document-redline-html/write-inline-document-redline-html.zip), then `dotnet run`.

Writes a self-contained `redline.html` and prints:

```text
Saved redline.html
```

## Full program

The zip contains these two inputs, each ending with a newline:

| `before.txt` | `after.txt` |
|---|---|
| Please submit the draft on Monday. | Please submit the draft on Friday. |

The program writes both words inline, then prints the comparison change ID and the opening tags of the corresponding marks in this sample.

```csharp
using OneDiff.Documents;
using System.Text.RegularExpressions;

var comparison = Comparison.Of("before.txt", "after.txt");
comparison.SaveAs("redline.html", RedlineForm.Html);
Console.WriteLine("Saved redline.html");

foreach (var change in comparison.Changes)
    Console.WriteLine($"Change {change.Id}");

var html = File.ReadAllText("redline.html");
foreach (Match tag in Regex.Matches(html, "<(ins|del) data-change=\"([0-9]+)\""))
    Console.WriteLine($"<{tag.Groups[1]} data-change=\"{tag.Groups[2]}\">");
```

```text
Saved redline.html
Change 0
<del data-change="0">
<ins data-change="0">
```

Open `redline.html` in a browser: “Monday” is struck through and “Friday” is underlined. In the element inspector, `data-change="0"` links both marks back to `comparison.Changes` by ID. The program’s regular expression only inspects this example’s `ins` and `del` tags; it isn’t a parser for every kind of change.

If an input path doesn’t exist, `Comparison.Of` throws before the program calls `SaveAs`; use the paths to your two input files in old-to-new order.

## Variations

Replace the `SaveAs` call in the snippet at the top:

**Use highlights and `+`/`-` marks** while keeping the inline layout. Writes `review.html`; update the filename in `Console.WriteLine` to match.

```csharp
comparison.SaveAs("review.html",
    RedlineForm.Html, RedlineStyle.Developer);
```

**Save the comparison data alongside the HTML.** Writes both `redline.html` and `changes.json`.

```csharp
comparison.SaveAs("redline.html",
    RedlineForm.Html);
comparison.SaveAs("changes.json");
```

## Related recipes

- [Render a text comparison as a PDF redline](https://recursiveai.org/onediff/cookbook/render-text-document-redline-as-pdf/)
- [Create a side-by-side HTML line diff](https://recursiveai.org/onediff/cookbook/side-by-side-html-diff/)
- [Export a document comparison to JSON](https://recursiveai.org/onediff/cookbook/export-document-comparison-json/)

## The short answer

Use `Comparison.Of` to compare the old and new documents and `SaveAs` with `RedlineForm.Html` to write one HTML file. The inline layout follows the revised document’s structure and marks removed text where it belonged. The report includes its CSS and doesn’t run scripts or fetch assets, so you can open it in a browser offline. Match a mark’s `data-change` to `Change.Id` to link a visible edit back to the comparison that produced it.

## How do you show inserted and deleted text in HTML?

HTML has [an `ins` element for added text](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/ins) and [a `del` element for removed text](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/del). OneDiff uses both: a replacement can produce a `del` and an `ins` with the same ID, as the weekday example shows. Leave unchanged words unmarked. They give the reviewer a place to start reading without looking up the original paragraph.

Those two elements don’t cover every kind of edit. The [inline format](https://recursiveai.org/onediff/docs/output-html/#2-inline-layout) uses separate markup for formatting changes and for moves. A move has an old-place and a new-place marker, with in-page links between `mvf-N` and `mvt-N`, where `N` is the change ID. In contrast, a `data-change` attribute is a reference to the comparison object, not itself a browser fragment ID. The “Differences not shown” section summarizes changes that the comparison settings leave out of the marked text.

## What’s the difference between an inline diff and a side-by-side diff?

The unit of review here is a document block, such as a paragraph or list item, not a pair of physical lines in a text file. A changed word can appear with the rest of its paragraph, and a moved paragraph can link its two locations. The containing block has `data-new` and `data-old` references for its revised and original nodes; `data-change` on the edit tells you which change you are looking at. A side-by-side line diff puts the file lines in separate columns. Use that view when line position is the thing you care about. Use a document redline when the reader needs to follow edits in reading order, and keep the JSON alongside the HTML when another program needs the comparison data rather than the display.
