Xml Editor Control


If you have a Windows Forms application that involves Xml editing or viewing, you can use this control to save yourself the effort of formatting the Xml content. For now, only syntax highlighting is implemented. I expect to add more features in the future like spacing, grouping, intellisense, etc…

Usage

Simply add the files (XmlToken.cs, XmlTokenizer.cs, XmlEditor.cs, XmlEditor.designer.cs) to your project then drag and drop the XmlEditor control from the Toolbox into your Windows Form.

The XmlEditor control currently has three public properties. Use AllowXmlFormatting to enable or disable formatting on the Xml content in the editor. The ReadOnly property tells whether or not to allow the user to change the text. The Text property sets or gets the text of the Xml editor.

Here is how the control looks like when AllowXmlFormatting = true and ReadOnly = false (default values):

Implementation

To color the Xml string, we have to split it into multiple tokens, then color each token based on its type. I have identified the following token types (based on syntax highlighting behavior in Visual Studio 2008):

  • A “Value” is anything between double quotes
  • A “Comment” is anything that starts with <!– and ends with –> (or starts with <!– and is never closed with –>)
  • An “Element” is any letter or digit that falls between < and a space or >
  • An “Attribute” is any letter or digit that falls after a < followed by space and not closed by >
  • An “Escape” is anything that starts with & and ends with ; (For example &quote;)
  • A “SpecialChar” is any character that is not a letter or a digit
  • A “None” is anything else

The Tokenize() public static method of the XmlTokenizer class does the job of splitting a string into Xml tokens.

An XmlToken object is a representation of an Xml token with details about the exact text of that token, its location in the string and its type.

Here is the code in the XmlEditor control that does the syntax highlighting:

List<XmlToken> tokens = XmlTokenizer.Tokenize(xmlEditor.Text);

foreach (XmlToken token in tokens)
{
    xmlEditor.Select(token.Index, token.Text.Length);

    switch (token.Type)
    {
        case XmlTokenType.Attribute:
            xmlEditor.SelectionColor = Color.Red;
            break;
        case XmlTokenType.Comment:
            xmlEditor.SelectionColor = Color.DarkGreen;
            break;

        //  and so on for the other token types
    }
}

Download source and exe.

About these ads
    • maria judi
    • November 15th, 2010

    thak u_______

    ths is very useful for my project

  1. Glad it is..

    • Marco van Tintelen
    • January 10th, 2011

    Very nice!

    • rishi
    • February 9th, 2011

    intellisense: do you have support for that ? If not can you tell me how can I implement it. thanks in advance.

  2. Nice work! Are you okay if I start with this and extend it or is this “as-is” only?

    Thank you!

    Colby

  3. Sure… Please share the final version that you come up with.

    If you are planning to distribute it, it’s OK as long as you keep reference that it was originally created by me and link to this post in the source code…

    • Joseph Gordon
    • August 17th, 2011

    Great work here. Small performance tip: you have a re-entry issue in your FormatXml method, where changing the selectionColor fires the textbox_changed event, causing another call to FormatXml.

    • W
    • February 17th, 2012

    Thanks. This is really helpful.

  4. Heya just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it
    in two different internet browsers and both show the same results.

  5. This is very interesting, You are a very skilled blogger. I have joined your
    feed and look forward to seeking more of your magnificent post.
    Also, I have shared your web site in my social networks!

  6. Thank you for every other informative web site.
    Where else could I get that type of info written in such a perfect way?

    I have a venture that I’m just now operating on, and I have been on the look out for such info.

  7. If possibly you paint your head’s hair, you may obtain a cost-effective
    hair painting for any couple of bucks, its surely not high-priced in any way, then you may really start off searching great!
    . If you ever generally have the very head of hair coloration then thats a plus and a issue a lot less to bother with.
    In the future you may wish to discover the crucial naruto costume, due to the surplus head of hair which it is actually got all around the throat, you will get a thing kind a
    sister or simply a cosing or almost certainly some girl it
    becomes clear that seems to have purchased a sweater or some element much like what has got the unique head of
    hair details, next the only dilemma leftover may be to position several orange attire have on more than this to be positive the hairs go on top of
    the top rated of this orange t-shirts or dresses making it look
    and also believe that this had been a particular.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: