Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / Windows Forms

XML Editor Control

Rate me:
Please Sign up or sign in to vote.
4.47/5 (11 votes)
2 Apr 2010CPOL2 min read 84.8K   27   25
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:

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

You can look at the code on my GitHub page.


Filed under: csharp, WinForms, XML

This article was originally posted at http://mycodelog.com/2010/04/01/xml-editor

License

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


Written By
Software Developer
United States United States
https://open-gl.com

Comments and Discussions

 
GeneralWorks great for viewing, needed some tweaks for editing Pin
Fr33dan6-Apr-20 5:18
Fr33dan6-Apr-20 5:18 
QuestionThanks Pin
Member 1025158131-Aug-17 1:16
Member 1025158131-Aug-17 1:16 
QuestionPerformance Pin
Member 1139632521-Dec-16 21:26
Member 1139632521-Dec-16 21:26 
GeneralThanks Pin
Ritesh Man Chtirakar23-Sep-15 19:03
Ritesh Man Chtirakar23-Sep-15 19:03 
GeneralRe: Thanks Pin
Ali BaderEddin8-Oct-15 8:11
Ali BaderEddin8-Oct-15 8:11 
GeneralMy vote of 1 Pin
Grumpyman31-Jan-15 6:21
Grumpyman31-Jan-15 6:21 
GeneralRe: My vote of 1 Pin
Ali BaderEddin31-Jan-15 13:40
Ali BaderEddin31-Jan-15 13:40 
QuestionAdd this to auto complete tags Pin
poteb7-Aug-12 3:21
poteb7-Aug-12 3:21 
AnswerRe: Add this to auto complete tags Pin
Ali BaderEddin7-Aug-12 7:12
Ali BaderEddin7-Aug-12 7:12 
QuestionThanks for sharing Pin
hbehar5-Jul-12 1:21
hbehar5-Jul-12 1:21 
QuestionPass text to control for formatting Pin
ijourneaux25-Dec-11 5:48
ijourneaux25-Dec-11 5:48 
QuestionHighlighting is very slow Pin
mubed1-Dec-11 1:28
mubed1-Dec-11 1:28 
AnswerRe: Highlighting is very slow Pin
Ali BaderEddin2-Dec-11 19:41
Ali BaderEddin2-Dec-11 19:41 
GeneralMy vote of 5 Pin
AndyTanYuLin28-Nov-11 20:16
AndyTanYuLin28-Nov-11 20:16 
QuestionGreat tool - When is next update Pin
zzfive00320-Sep-11 7:48
zzfive00320-Sep-11 7:48 
Ali;

Very nice work, i might use this for something i am doing, where i need a user to modify XML instructions.

Do you have the new version that does space and indent? That is the only real thing missing.

Otherwise, very neat, and easy to use.

Thanks!
Mark
AnswerRe: Great tool - When is next update Pin
Ali BaderEddin24-Sep-11 18:14
Ali BaderEddin24-Sep-11 18:14 
QuestionNice Pin
shelby6716-Aug-11 18:22
shelby6716-Aug-11 18:22 
AnswerRe: Nice Pin
Ali BaderEddin20-Aug-11 21:50
Ali BaderEddin20-Aug-11 21:50 
GeneralCrashed with lots of characters Pin
zhanghaocol25-Oct-10 22:06
zhanghaocol25-Oct-10 22:06 
GeneralRe: Crashed with lots of characters Pin
Ali BaderEddin26-Oct-10 11:03
Ali BaderEddin26-Oct-10 11:03 
GeneralUndo is completely broken Pin
nikdownload25-Sep-10 6:44
nikdownload25-Sep-10 6:44 
GeneralRe: Undo is completely broken Pin
Ali BaderEddin24-Oct-10 10:17
Ali BaderEddin24-Oct-10 10:17 
GeneralWeb based XML Editor Control Pin
UdayanDas22-Jul-10 0:00
UdayanDas22-Jul-10 0:00 
GeneralGreat control Pin
ruben ruvalcaba6-Apr-10 4:16
ruben ruvalcaba6-Apr-10 4:16 
GeneralRe: Great control Pin
Ali BaderEddin6-Apr-10 7:23
Ali BaderEddin6-Apr-10 7:23 

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

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