Free Support Forum -

Comparer.Compare API extension for string input and output parameters

We are doing a POC and exploring the capabilities of this diffing solution.
Since we are using comparisons on a web application on the fly, a major issue for us has been that the Comparer.Compare() method does not support string inputs and string output, the result of the files compared. We do not want to write the result in a file but have it returned as a string from the method.

In our case, we are comparing 2 XHTML/HTML files, with some xpaths ignored.
So ideally, we would like to be able to pass in 2 strings and a collection (new file, old file, xpaths to ignore)
The ideal output would be to have the result of the comparison with annotations for changes in string format and also a collection of those changes.

We have managed to go around the inputs by converting the string into a stream and doing some pre-processing for xpaths to ignore, but we haven’t got around reading the file stream as a string.

    private static (bool changesFound, string htmlDiffWithAnnotations) Execute(string baseDocumentHtml, string revisedDocumentHtml)
        var options = new CompareOptions
            InsertedItemStyle = new StyleSettings
                IsUnderline = true,
                FontColor = Color.ForestGreen,
            DeletedItemStyle = new StyleSettings
                IsStrikethrough = true,
                FontColor = Color.DarkRed,
            DetalisationLevel = DetalisationLevel.Low,
            SensitivityOfComparison = 0,

        var comparer = new Comparer(GetStreamFromString(baseDocumentHtml));

        var outputStream = new MemoryStream();
        comparer.Compare(outputStream, options);

        outputStream.Seek(0, SeekOrigin.Begin);
        var streamReader = new StreamReader(outputStream);
        var htmlDiffWithAnnotations = streamReader.ReadToEnd();

        return (comparer.GetChanges().Any(), htmlDiffWithAnnotations);

    private static Stream GetStreamFromString(string documentString)
        var bytesArray = Encoding.UTF8.GetBytes(documentString);
        return new MemoryStream(bytesArray);

Was wondering if there are future considerations extending the API to make the comparator suitable for comparisons on the fly without having to create a file or a file server.


1 Like


We are investigating this scenario. Your investigation ticket ID is COMPARISONNET-2630. You’ll be notified in case of any update.


As for ability to pass not a file but strings and get a result as a string:
We could do this using logic for comparing plain text files (so result of changes would be like when comparing text files). Please do an experimental comparison of text files and let us know if this is an appropriate result for you?
As for point about managing comparing process with Xpath string, it’s not fully clear for us. How exactly should it be? It’s quite complicated functionality and we need to know what you are actually expecting?


Could you provide an example how would you change the below piece of code to accept 2 strings (new text, old text) and return comparison result text, all of type string?

var comparer = new Comparer("new text goes here");
comparer.Add("old text goes here");
string compariosonResult;
comparer.Compare(compariosonResult, options); 

The ideals solution would be:

var xpathsToExcludeFromComparison = new string[]

var oldText = "<div>abc</div>";
var newText = "<div>abd</div>"

var comparer = new Comparer();
var comparisonResult = comparer.Compare(oldText, newText, xpathsToExcludeFromComparison);

comparisonResult.resultWithAnnotations; // the result of comparison as string
comparisonResult.Changes; // is a collection of changes objects, currently supported in your solution
1 Like


Thank you for sharing the details. We’ll further look into this scenario.

1 Like