XML Schema Validator

This is extremely trivial XML Schema Validator; I just needed to verify my XML against a Schema File. Very simple code but maybe helpful to someone.

using System;

using System.Xml;

using System.IO;

using System.Xml.Schema;

namespace XmlConsole


/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1


private static int errors = 0;

/// <summary>

/// The main entry point for the application.

/// </summary>


static void Main(string[] args)




// The Source XML File

                        XmlTextReader xtr = new XmlTextReader(“C:\\devresult.xml”);

                        XmlValidatingReader reader = new XmlValidatingReader(xtr);

                        reader.ValidationType = ValidationType.Schema;

// The Path of the XML Schema File against which the Validation is to be made.

// you can add more than one Files by using reader.Schemas.Add()

                        reader.Schemas.Add(null,new XmlTextReader(@”E:\Schema.xsd”));

                        reader.ValidationEventHandler += new ValidationEventHandler(reader_ValidationEventHandler);




if(errors == 0)

                              System.Console.WriteLine(“Validation Successfull”);


                              System.Console.WriteLine(“Validation Failed”);



catch(Exception exp)



                        errors ++;



private static void reader_ValidationEventHandler(object sender, ValidationEventArgs e)







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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s