Understanding XML Validation




Validation is the process of enforcing rules on the XML content either via a DTD, a XSD schema or a
XDR schema. An XML file is generally validated for its conformance to a particular schema or a DTD.
For example, if you specify the age of an employee to be an integer data type in the schema of your XML
document, the actual data in your XML document must conform to the data type or it will be considered
invalid. The XML schema file usually is an XML-Data Reduced (XDR) or XML Schema Definition
language (XSD) file. XSD schema-based validation is the industry accepted standard and will be the
primary means of validating XML data in most of the newly developed .NET applications.

You can perform the validation of XML documents by using the validation settings supplied with the

System.Xml.XmlReaderSettings class. The XmlReaderSettings class provides the DTD and XSD
schema validation services that allow you to validate an XML document or a fragment of an XML
document. The Create method of the XmlReader class takes an XmlReaderSettings object as one of the
inputs and applies the properties that you specify in the XmlReaderSettings class while reading the
XML document.

The following code shows how to use the XmlReaderSettings class to add validation support to the

XmlReader class.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += new

ValidationEventHandler(this.ValidationEventHandler);

settings.ValidationType = ValidationType.Schema;   
settings.Schemas.Add(null, XmlReader.Create(“Employees.xsd”));
reader = XmlReader.Create(“Employees.xml”, settings);
while(reader.Read()){}

To validate an XML document, you first create an instance of the XmlReaderSettings class and assign an
event handler for the ValidationEventHandler event. You then set the ValidationType enumeration
to ValidationType.Schema to indicate that you are validating the XML data against the XSD schema.
Next, load the XSD schema object utilizing the Add() method of the XmlSchemaSet class and then supply
the XmlReaderSettings object to the Create() method of the XmlReader class. For in-depth information
on XML Data validation, please refer to Chapter 5.

Transforming XML Data using XSLT

XSLT is the transformation component of the XSL specification by W3C (www.w3.org/Style/XSL). It is
essentially a template-based declarative language that can be used to transform an XML document to
another XML document or to documents of other types (such as HTML and Text). You can develop and
apply various XSLT templates to select, filter, and process various parts of an XML document. Figure 3-1
demonstrates the XSL transformation process.