Click here to Skip to main content
15,115,667 members
Articles / Programming Languages / XML
Posted 1 Oct 2004


28 bookmarked

A Simple XML Validator

Rate me:
Please Sign up or sign in to vote.
4.75/5 (10 votes)
1 Oct 20042 min read
A simple application for validating XML files from within Windows Explorer.

Sample Image - xmlvalidator.png


When using XML, it's very useful to be able to validate your XML. Often, it's tedious to load your XML into an XML editing package and validate the XML using its built in tools. This article describes a simple lightweight tool for validating XML files via right clicking on them in Explorer.

Integrating into Explorer

One of the main goals of writing this application was to allow me to easily check if the syntax of XML files is valid. What better way of doing this than by right clicking on them within Windows Explorer.

To integrate in to Explorer, I used the code detailed at Nish's blog.

When the application is executed, it checks to see what command line arguments are passed to it. If the argument /install is found, then the application registers itself in the registry against XML files.

if ((args.Length == 1) && (args[0] == "/install"))
    ContextMenu cm = new ContextMenu();
    bool ok = cm.AddContextMenuItem(".xml", "Validate", 
              "Validate XML", "\""+Application.ExecutablePath + "\" %1");
    if ( ok == true )
        MessageBox.Show("The application has been installed.");
        MessageBox.Show("The application could not be installed.");

XML Validation

Once the application is integrated into Windows Explorer, it takes one command line parameter which is the name of the XML file to validate. The file is validated using a XMLValidatingReader as shown in the code below.

public class XMLValidator
    private String fileName;
    static private ArrayList errors = new ArrayList();
    static private bool bValid;

    public XMLValidator(String fileName)
        this.fileName = fileName;
        bValid = true;

    public ArrayList GetErrors()
        return errors;

    public bool Validate()
        XmlValidatingReader reader=null;
            bValid = true;
            XmlTextReader txtreader = new XmlTextReader (fileName);
            reader = new XmlValidatingReader (txtreader);

            // Set the validation event handler
            reader.ValidationEventHandler += 
                   new ValidationEventHandler (ValidationCallBack);

            // Read XML data
            while (reader.Read()){}

        catch (Exception e)
            bValid = false;
            //Close the reader.

        return bValid;

    private void ValidationCallBack (object sender, ValidationEventArgs args)
        bValid = false;


The main work carried out in this class is done in the Validate() method. You can see that this instantiates a XmlValidatingReader object. This class can validate against DTD, XDR and XSD schemas. After instantiating an XmlValidatingReader, the code specifies a ValidationEventHandler. This event handler is called when errors are found in the XML. All this handler does therefore is log any error to a list so that they can be displayed. Finally, the class reads through all the lines in the specified XML file using the read() method.

Running the application

Running the application is really easy. First of all, you need to make the link in the registry by running the app with the /install switch. Then simply right click on any XML file in Explorer and choose the "Validate XML" option.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Salter
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
Dan Glass11-Nov-09 20:01
MemberDan Glass11-Nov-09 20:01 
General[Message Removed] Pin
hankjmatt13-Oct-08 23:22
Memberhankjmatt13-Oct-08 23:22 
GeneralThank you so much! Pin
RossMcLoughlin13-Jun-07 4:22
MemberRossMcLoughlin13-Jun-07 4:22 
GeneralXML validation without reading / writing it to disk Pin
Alonr10-Dec-06 6:32
MemberAlonr10-Dec-06 6:32 
GeneralHandle Pathnames with spaces Pin
Jeff Firestone3-May-05 18:59
MemberJeff Firestone3-May-05 18:59 
GeneralFeedback on XML Validator Pin
eddie20467-Oct-04 5:28
Membereddie20467-Oct-04 5:28 
GeneralRe: Feedback on XML Validator Pin
David Salter8-Oct-04 0:08
MemberDavid Salter8-Oct-04 0:08 

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.