This code in C# should do just that - it will scan the entire XML and validate it against the XSD provided, and it will spit out all validation errors (or errors in the schema, too!) as they happen. Hope this helps!
The way you use it would be:
MyXmlValidator (name of XML file) (name of XSD file)
That's all there is!
Marc
static void Main(string[] args)
{
ShowTitle();
if(args.Length < 2)
{
ShowUsage();
return;
}
string xmlFileName = args[0];
string xsdFileName = args[1];
if(!File.Exists(xmlFileName))
{
ShowError(string.Format("XML File '{0}' does not exist)", xmlFileName));
return;
}
if (!File.Exists(xsdFileName))
{
ShowError(string.Format("XSD schema '{0}' does not exist)", xsdFileName));
return;
}
StreamReader xsdReader = new StreamReader(xsdFileName);
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.ValidationType = ValidationType.Schema;
ReaderSettings.Schemas.Add(Schema);
ReaderSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);
XmlTextReader xmlReader = new XmlTextReader(xmlFileName);
XmlReader objXmlReader = XmlReader.Create(xmlReader, ReaderSettings);
while (objXmlReader.Read())
{ }
Console.WriteLine("Successful validation completed!");
}
private static void XSDValidationEventHandler(object sender, ValidationEventArgs e)
{
Console.WriteLine("Error reading XSD: " + e.Message);
}
private static void XMLValidationEventHandler(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation error: " + e.Message);
}
private static void ShowError(string message)
{
Console.WriteLine("ERROR: " + message);
}
private static void ShowUsage()
{
Console.WriteLine("USAGE: GaraioXmlValidator (name of XML file) (name of XSD file)" + Environment.NewLine);
}
private static void ShowTitle()
{
Console.WriteLine("GaraioXmlValidator v1.0 (c) 2008 by Garaio Technology Lab" + Environment.NewLine);
}