I am trying to compare two XML files, each put into a different RichTextBox, in VB.NET. The first RichTextBox will have the original XML file, while the second RichTextBox will have an updated XML file. I want to highlight the differences between the two files in the second RichTextBox.
Currently, I simply put each row of text from the original XML file into a List(Of String). I then loop through all the lines of the second RichTextBox and if the List(Of String) does not contain that line, then I know the line was either added (a new line) or was edited, so it has to be highlighted in the second RichTextBox.
This is the code:
Dim sRichTextBox1Lines() As String = RichTextBox1.Lines
Dim sRichTextBox2Lines() As String = RichTextBox2.Lines
Dim sLines As List(Of String) = New List(Of String)
Dim sRichTextBox2Line As String = ""
Dim nIndexStartOfCurrLine As Integer = 0
For i As Integer = 0 To (sRichTextBox1Lines.Length - 1)
sLines.Add(sRichTextBox1Lines(i))
Next
For i As Integer = 0 To (sRichTextBox2Lines.Length)
sRichTextBox2Line = sRichTextBox2Lines(i).ToString
nIndexStartOfCurrLine = RichTextBox2.GetFirstCharIndexFromLine(i)
If (Not sLines.Contains(sRichTextBox2Line)) Then
RichTextBox2.Select(nIndexStartOfCurrLine, sRichTextBox2Lines(i).Length)
RichTextBox2.SelectionColor = Color.White
RichTextBox2.SelectionBackColor = Color.Red
End If
Next
This does the job. However, I was thinking about only highlighting the actual differences, such as a space added, rather than simply highlighting the whole line.
Now this is where I am having issues. I was thinking of going line-by-line and comparing each line to highlight only the difference between the two lines, but there are issues with this approach (e.g. a line was removed, a line was commented out and modified on the next line, a blank line was added, etc.) and the code becomes very convoluted and hard to follow. For example, suppose this is in the first XML file (RichTextBox1):
<add key="Test123A" value="123A"/>
<add key="Test123B" value="123B"/>
<add key="Test123C" value="123C"/>
<add key="Test123D" value="123D"/>
And suppose this is in the second XML file (RichTextBox2):
<add key="Test123A" value="123A"/>
<!--<add key="Test123B" value="123B"/>-->
<add key="Test123B" value="456B" />
<add key="Test123C" value="123C" />
<add key="Test123D" value="123D" />
A line-by-line comparison would be messed up since lines 2 and 3 do not match anything in the original XML file. Also, note that there is now a space after the last quotation marks in the new XML file, which isn't a huge difference (literally a 1-char difference). I was thinking about taking SubStrings but some other parts of the XML file could have the same text, making things complicated.
Perhaps I'm overthinking this but I'm just struggling to find a nice method to highlight only the differences rather than the whole line.