Click here to Skip to main content
Click here to Skip to main content
Technical Blog

XML Editor Control

, 2 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
XML Editor with syntax highlighting

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 XMLeditor.

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
    }
}

Filed under: csharp, WinForms, XML

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ali BaderEddin
Software Developer Microsoft
United States United States
http://mycodelog.com/about/

Comments and Discussions

 
QuestionAdd this to auto complete tags Pinmemberpoteb7-Aug-12 4:21 
AnswerRe: Add this to auto complete tags PinmemberAli BaderEddin7-Aug-12 8:12 
QuestionThanks for sharing Pinmemberhbehar5-Jul-12 2:21 
QuestionPass text to control for formatting Pinmemberijourneaux25-Dec-11 6:48 
QuestionHighlighting is very slow Pinmembermubed1-Dec-11 2:28 
AnswerRe: Highlighting is very slow PinmemberAli BaderEddin2-Dec-11 20:41 
GeneralMy vote of 5 PinmemberAndyTanYuLin28-Nov-11 21:16 
QuestionGreat tool - When is next update Pinmemberzzfive00320-Sep-11 8:48 
AnswerRe: Great tool - When is next update PinmemberAli BaderEddin24-Sep-11 19:14 
QuestionNice Pinmembershelby6716-Aug-11 19:22 
AnswerRe: Nice PinmemberAli BaderEddin20-Aug-11 22:50 
GeneralCrashed with lots of characters Pinmemberzhanghaocol25-Oct-10 23:06 
GeneralRe: Crashed with lots of characters PinmemberAli BaderEddin26-Oct-10 12:03 
GeneralUndo is completely broken Pinmembernikdownload25-Sep-10 7:44 
GeneralRe: Undo is completely broken PinmemberAli BaderEddin24-Oct-10 11:17 
GeneralWeb based XML Editor Control PinmemberUdayanDas22-Jul-10 1:00 
GeneralGreat control Pinmemberruben ruvalcaba6-Apr-10 5:16 
GeneralRe: Great control PinmemberAli BaderEddin6-Apr-10 8:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by Ali BaderEddin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid