<h4>How Do I...Apply validation when reading XML?</h4>
<div class="indent" style="width:660">
This sample illustrates how to apply validation when reading XML data using the XMLTextReader class. Validation is the process of enforcing rules on the XML
content either via a Document Type Definition (DTD) or a schema. At it's simplest, a schema is an XML representation of a DTD, but can also infer relationships
within the XML data.<br clear="left"><br>
This sample follows on from the <a href="/quickstart/howto/doc/Xml/ReadXmlFile.aspx">How do I...Read XML from a file?</a> topic.
In this sample we are going to apply validation when reading and parsing XML data using sample files which pass and fail.
The XmlTextReader can provide either DTD or XDR parse time validation. The <b>Validation</b> property can be used to request a particular validation option.
XDR is the Microsoft XML-Data Schema (which is a pre-cursor to the W3C XML schema) and is called the reduced set of XML Schema (also referred to as
XML-Data Reduced). This is described at <A target=_top href="http://www.ltg.ed.ac.uk/~ht/XMLData-Reduced.htm">XML-Data Reduced</A>.
The table below shows the types of validation which can be set.</div>
<td width="509" height="19"><font size="1">Whether to validate according to an XDR schema.</font></td>
</tr>
</TBODY></TABLE></DIV>
<br clear="left"><br>
<div class="indent" style="width:660">
The Auto mode does the following:
<LI>If there is no DTD or schema then it will parse the XML without validation.
<LI>If there is a DTD defined in a <!DOCTYPE ...> declaration then it will load the DTD and process the DTD declarations.
<LI>If there is no <!DOCTYPE ...> declaration but XDR schemas are specified (using the "x-schema:" prefix), then it loads and process those schemas. </LI></UL></DIV>
four files are validated. The first BooksDTD.xml validates against the books.dtd, the second BooksDTDFail.xml fails validation against the books.dtd,
the third BooksSchema.xml validates against the schema.xml file and the fourth BooksFailSchema.xml fails validation against the schema.xml.
Schema.xml is an XDR schema. To set the required type of validation, the <b>Validation</b> property is set either with the value of <b>DTD</b>
or <b>Schema</b> as illustrated below.</div>
<div class="code"><xmp>
XmlTextReader m_reader = new XmlTextReader (m_Document1);
m_reader.Validation = Validation.Schema;
m_reader.Validation = Validation.DTD;
</xmp></div>
<div class="indent" style="width:660">
Errors in validation result in the <b>ValidationCallback</b> method being called. This property is set with a <b>ValidationDelegate</b> class before reading the
XML document. If this is not provided then no validation errors are returned and parsing only stops if an XML well-formedness error occurs. However, if no
ValidationDelegate is provided you get default attributes and entities without having to deal with validation errors. The sample source code illustrates this.</div>
<div class="code"><xmp>
private void Validate()
{
try
{
// Set the validation callback
m_reader.ValidationCallback = new ValidationDelegate(this.ValidationCallback);
In this code sample, the class m_success variable indicates the validation state. The parser does not stop for any kind of validation error and only stops if
there is a well-formedness error during the read. This feature enables you find all the validation errors in one pass without having to continously re-parse
Validation error: Content is incomplete.Line: 4 Position: 4
Validation error: Content is incomplete.Line: 4 Position: 4
Validation finished. Validation failed
</xmp>
</div>
<H4>Summary</H4>
<OL>
<LI>Validation can either be against a DTD or an XDR schema, the type required being set by the Validation property.
<LI>Validation is performed during the read and parsing operations.
<LI>The ValidationCallback property must be set with a ValidationDelegate class to receive notification of validation errors.
<LI>Validation errors do not stop parsing. Parsing only stops if there is a well-formedness error. This enables all errors to be discovered in a single pass.