I wrote the following code to vaidate an xml file named order.xml
private void ValidateOrderXml()
{
// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection.
sc.Add("urn:order-schema", "order_schema.xsd");
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(sc);
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create(xmlFileName, settings);
// Parse the file.
try
{
while (reader.Read()) ;
}
catch (XmlException err)
{
;
}
finally
{
reader.Close();
}
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation Error: {0}", e.Message);
}
But the problem is the code does not give any error even if the xml file is invalid. For example,according to schema, the root node should have name orders. I used an xml file with some other root name. Still it validated.