Lets assume that the XML file that we wish to validate has details about orders. Each of the Order contains the Order ID, details about the Customer, Discount percent given and the details about Products. Following graphic(click for larger image) shows the XML file that we are using:
Orders.xml
To validate the XML shown above, we have the schema. The schema for the XML can be found here.
Validating XML using XmlValidatingReader:
To validate the XML file against a schema, we can use XmlValidatingReader. The steps involved are:- Create instance of XmlValidatingReader using the XML file to validate.
- Create an object of XmlSchema using the schema file to validate against.
- Hookup event for XmlValidatingReader.ValidationEventHandler.
- Read the Xml to the end using XmlValidatingReader.
using (XmlValidatingReader xmlValidatingReader = new XmlValidatingReader(new XmlTextReader("Orders.xml")))
{
// Create the schema object to validate XML files
XmlSchema xmlSchema = XmlSchema.Read(new XmlTextReader("Orders.xsd"), new ValidationEventHandler(Schema_ValidationError));
// Add to the collection of schemas for XmlValidatingReader
xmlValidatingReader.Schemas.Add(xmlSchema);
// Attach an event which will be filed on validating error
xmlValidatingReader.ValidationEventHandler += new ValidationEventHandler(xmlValidatingReader_ValidationEventHandler);
// Read the XML to the end
while (xmlValidatingReader.Read()) ;
Console.WriteLine("\nFinished validating XML file....");
}
Validating XML using XmlReaderSettings:
Validating XML using XmlReaderSettings follows almost the same path as that for XmlValidatingReader, with a difference that here we will be using XmlReaderSettings to pass in the schema details. Following are the steps to using XmlReaderSettings for validating an XML file:
- Create a XmlSchema object using the schema file.
- Create an object for XmlReaderSettings.
- Set the ValidationType of XmlReaderSettings as "Schema".
- Add the XmlSchema to the collection of schemas of XmlReaderSettings.
- Attach the event for XmlReaderSettings.ValidationEventHandler.
- Create an instance of XmlReader using XmlReader.Create() and passing in XML file to validate.
- Read the XmlFile to the end.
// Create the schema object
XmlSchema xmlSchema = XmlSchema.Read(new XmlTextReader("Orders.xsd"), new ValidationEventHandler(Schema_ValidationError));
// Create reader settings
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
// Set validation type to schema
xmlReaderSettings.ValidationType = ValidationType.Schema;
// Add to the collection of schemas in readerSettings
xmlReaderSettings.Schemas.Add(xmlSchema);
// Attach event handler whic will be fired when validation error occurs
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(xmlReaderSettings_ValidationEventHandler);
// Create object of XmlReader using XmlReaderSettings
using (XmlReader xmlReader = XmlReader.Create(new XmlTextReader("Orders.xml"), xmlReaderSettings))
{
// Read XML to the end
while (xmlReader.Read()) ;
Console.WriteLine("\nFinished validating XML file....");
}
The code behaves the same in both the cases. However, there is a small difference in how the exception message is displayed. Using theXmlValidatingReader tells about the element/attribute with incorrect value as per the data type, but using XmlReaderSetting provides a detailed error showing the incorrect value along with the expected datatype.
I hope I was able to provide most of you with a good insight on validating XML files using schemas. If you have been using some other method, please do share it here.