XMl Data Validation

Introduction

 
The XML text reader class does not enable us to validate the content of the XML source against a schema. The XML document can be measured using two distinct metrics. Validation involves the semantics of the document, and it should be compliant with the user-defined layouts. The class ensures the document is correct and the class avoids the more advanced analysis of nodes in the document. 
 

XML validatingReader class

 
This class is used to implementation of the xmlReader class which provides support in XML validation. The class is used to validate XML documents as XML fragments. It is a string of XML code that does not have a root node. An XML document should have a root node.
 

Supporting Validation Types

               
DTD
 
It is a text file whose syntax stems are directly from standard generalized markup language. It follows a non-XML syntax to define a set of valid tags. It allows us to specify the children for each tag, and few other properties.
 
XDR
 
It is a schema language and it is flexible and overcome the limitation of DTD. All data content is character data, XDR language allows to specify the data type of an element.
 
XSD
 
It defines the elements and attributes that form an XML document and each element is strongly typed. It is composed of primitive and derived types. 
 

Validation Event Handler Event

 
The class contains a public event named validation Event handler and this event is used to pass information about schema validation error that is deleted. The handler for the event has the following signature:
  1. public delegate void ValidationEventHandler (    
  2.  object sender ,     
  3.  validationEventArgs e    
  4. ) ;    
The class described by the below code and the message field displays the description of the error. The exception field returns the exception object. The exception class contains information about the error. Error is a validation error occurred during processing the document and warning is to validate a particular element or attribute.
  1. public class ValidationEventArgs : EventArgs    
  2. {    
  3.   public XmlSchemaException exception;    
  4.   public string Message;    
  5.   public XmlSeverityType severity;    
  6. }     

XML ValidatingReader in Action

 
It is a reader class and it proceeds with incremental validation as nodes. The validationEventHandler event is raised to caller is notified of any schema exception found. To validate the content of XML file, first create an XML text reader to work file and use reader to initialize instance of validating reader. It can be initialized by living instance of xmlReader classs. The below content is looped to validate the XML document,
  1. public bool ValidateDocument ( string fileName )    
  2. {    
  3.  XmlTextReader _coreReader = new XmlTetReader ( fileName );    
  4.  XmlValidatinngReader reader = new XmlVlidatingReader ( _coreReader );    
  5. reader.ValidationType = ValidationType.Auto;    
  6. reader.ValidationEventHandler += new ValidationEventHandler ( MyHandler );    
  7.  while (reder.Read() )    
  8. {    
  9. }    
  10. reader.Close();    
  11. return.true;    
  12. }    

Handling Validation Errors

 
To know details of validation error, it needs to define the event handler and pass it to the validation reader. The reader fires an event and continues to parse when an error occurs. The validation Type property denotes the type of validation performed in the document and sets the property after the first call to read originate invalidOperationException exception. 
  • Auto - Determine the appropriate type of validation at the content of the document.
  • DTD - Validate according to DTD
  • Schema - validate according to the specified XSD scheme including line schemas.
  • XDR - Validate to XDR scheme including in-line schemas.
  • None - Create a nonvalidating reader and ignore any error invalidation.

Word on XML Dom

 
Creates XML DOM from a variety of sources with stream, text, reader, file name. The below target is shown when loading the document through an XML validating reader.
  1. XmlTextReader _coreReader = new XmlTextReader ( fileName );      
  2. XmlValidatingReader  reader = new XmlValidatingReader ( _ coreReader ) ;      
  3. XmlDocument doc = new XmlDocument ();      
  4. doc.Load ( reader );      

Validating XML Fragments

 
The xmlvalidatingReader class has the ability to parse and validate documents as XML fragments. To parse XML fragments resort to one other two constructors the XmlValidatingReader clas provide. The constructor allows us to read XML fragments from a stream and process it with boundaries.