Jonas Rapp has a fantastic XrmToolBox Tool called the FetchXml Builder. I think I might have mentioned it once, or maybe twice, in past discussions. (If you have not used it, you should!) One cool component of the FetchXml Builder is the color coded XML viewer. This component would be great for a lot of other Tools, but if you check out the code, it’s a bit tough to implement.

Luckily, Jonas offered up the code for the XML viewer from his FetchXml Builder tool for a reusable XrmToolBox shared control. He wanted to see it wrapped up in a more reusable fashion for others to use.

How could I refuse the chance to add another bit of Rappen code to the Shared Control library?

A quick update: Thanks to Jonas for the heads up making sure credit goes to the right place. The code behind this control was originally published on MSDN as a sample and it’s stood the test of time! Check out the post here: XML syntax highlighting in RichTextBox (CSRichTextBoxSyntaxHighlighting)

XMLViewer

The XML viewer control is simply named XMLViewer and derives from the RichTextBox WinForm control. This is a nice model as we simply extend RichTextBox properties and methods rather than starting from scratch.

XMLViewer User Control

Under the hood, this control will parse your XML input, or the Text of the RTF, and wrap each XML element in RTF code that will then the RichTextBox control renders as color coded XML. We have some control over how we style the rendered XML when building the RTF via the Settings object.

Style Settings

When you add this control to your XrmToolBox tool, you will see a few additional properties in the designer property pane:

XMLViewer Property Page

The Settings object allows you to control the colors and layout options for your XML. These properties can be updated at runtime and the XMLViewer control should re-render the XML.

Parsing

By default, the XMLViewer will attempt to process the current text as XML as you type. You can turn this off by setting FormatAsYouType to false, calling the public Process method as needed to re-render the color coded XML.

If you want to simply display XML rather than allow a user input, you can lock down the XMLViewer control using the existing Read Only property. Then we can call the Process() method as needed.

Parse errors are available via the NotificationMessage event. This event provides a bit more detail on parsing our XML if you would like to display or respond to this error in some way.

Using this control within your XrmToolBox Tool, you have the option of providing controls that access the Settings or for calling the external methods such as Process. You also have the option of saving the Settings as part of your standard Tool settings.

XMLViewerControl

Not one, but TWO new controls!

If you would rather not add the controls for calling methods or accessing Settings, I wrapped up the XMLViewer in another control the creatively named XMLViewerControl. This control simply wraps up the XMLViewer and provides a toolbar for calling the Process() method and for changing your color and font settings at runtime.

XMLViewerControl with toolbar

You will also have access to some of the underlying RichTextBox properties as with the XMLViewer control, such as Read Only, Word Wrap, or setting the Text directly.

Another handy element to this wrapper user control is that you can choose to display the parse errors as you type:

XML Parse Errors

This notification might be useful if your Tool allows user input of XML directly, such as working on a FetchXml snippet or Grid XML.

You have the option of hiding both the toolbar and parse errors. Once hidden, this user control will operate much like the XMLViewer control.

Building this control was primarily a method for testing the XMLViewer public interface, but it may save you some time when building your own Tool.

Give it a try!

The code for the latest controls (and a few minor fixes) has been posted to the Shared Control Library on GitHub at https://github.com/jamesnovak/xrmtb.XrmToolBox.Controls, and the NuGet package has been updated to the latest version: https://www.nuget.org/packages/xrmtb.XrmToolBox.Controls

I’m working on the documentation for all of the controls in addition to the Code Only NuGet package, so I will be adding more detail on GitHub for these and the other controls soon.

As always, any and all feedback is welcome: drop a line via GitHub for any questions , issues, or suggestions!

0 Points


Leave a Reply

Your email address will not be published. Required fields are marked *