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>
[STAThread]
static void Main(string[] args)
{
try
{
// 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);
while(reader.Read());
xtr.Close();
reader.Close();
if(errors == 0)
System.Console.WriteLine(“Validation Successfull”);
else
System.Console.WriteLine(“Validation Failed”);
System.Console.ReadLine();
}
catch(Exception exp)
{
System.Console.WriteLine(exp.Message);
errors ++;
}
}
private static void reader_ValidationEventHandler(object sender, ValidationEventArgs e)
{
System.Console.WriteLine(e.Message);
}
}
}