Click here to Skip to main content
Click here to Skip to main content

A Simple XML Validator

, 1 Oct 2004
Rate this:
Please Sign up or sign in to vote.
A simple application for validating XML files from within Windows Explorer.

Sample Image - xmlvalidator.png

Introduction

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.");
    else
        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;
        try
        {
            errors.Clear();
            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;
            errors.Add(e.Message);
        }
        finally
        {
            //Close the reader.
            reader.Close();
        }

        return bValid;
    }

    private void ValidationCallBack (object sender, ValidationEventArgs args)
    {
        bValid = false;
        errors.Add(args.Message);
    }

}

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.

License

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

Share

About the Author

David Salter
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
General[Message Removed] Pinmemberhankjmatt13-Oct-08 22:22 

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 | Mobile
Web02 | 2.8.140827.1 | Last Updated 1 Oct 2004
Article Copyright 2004 by David Salter
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid