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));
comparer.Add(GetStreamFromString(revisedDocumentHtml));
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.
Thanks