Recently I’ve been looking for some piece of code that would perform XML highlighting in a Rich Text Box. The Internet provides several free samples and several commercial samples but I wasn’t really satisfied with what I found, mainly because it involved using an XMLTextReader object, which does not help for WYSWYG editing of the XML file.

So I created a simple C# function that formats the text content from a RichTextBox:

public static void HighlightRTF(RichTextBox rtb)

The complexity is roughly O( n ), where n is the number of characters in the XML file.

Here is the result:

image

You’ll find the function in the text attachment.