Read <ins> and <del> in a OneDiff report. The zip’s before.txt and after.txt change a fare from $8 to $9.
dotnet add package RecursiveAI.OneDiff.DocumentsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 and dotnet run.
Writes diff.html and prints:
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:
using OneDiff.Documents; var comparison = Comparison.Of( "before.txt", "after.txt"); comparison.SaveAs("diff.json");
See how to read changes back from JSON.
Related recipes
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; 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.