[C# – XML] Validation Settings

It is easy to use “XmlTextReader“and the class automatically checks whether the document is well-formed. But what if you want more such as validating XML using DTD or Schema?  In the .Net 1.1, the “XmlValidatingReader” is used. But it is obsolete in .Net 2.0.

 

1. XmlReaderSettings

To validate XML documents, you need to create an “XmlReaderSettings” object and pass it to the “XmlReader“.

The important properties of “XmlReaderSettings” are:

public ValidationType ValidationType { get; set; } // The default is ValidationType.None
public DtdProcessing DtdProcessing { get; set; } // The default is DtdProcessing.Prohibit
public XmlSchemaSet Schemas { get; set; }
public XmlSchemaValidationFlags ValidationFlags { get; set; }
public bool IgnoreComments { get; set; } // The default is false
public bool IgnoreProcessingInstructions { get; set; } // The default is false
public bool IgnoreWhitespace { get; set; } // ignore insignificant white space (The default is false)

 

2. ValidationType Property

To indicate the type of validation to perform, you need to set the “ValidationType” property

  • public ValidationType ValidationType {get; set;}

The “System.Xml.ValidationType” enum has the following values:

public enum ValidationType
{
  None, //no validation is performed, creates an XML 1.0 compliant non-validating parser
  Auto, // validates if DTD or schema information is found. (obsolete)
  DTD, // validates according to the DTD
  Schema, // Validates according to XSD schemas
  XDR  // Validates according to XDR schemas. (obsolete)
}

 

3. DtdProcessing Property

The “DtdProcessing” property is used to indicate whether DTD is used for validation.

  • public DtdProcessing DtdProcessing { get; set; }
public enum DtdProcessing
{
  Prohibit, // specifies that when a DTD is encountered, an XmlException is thrown with a message that states that DTDs are prohibited
  Ignore, // causes the DOCTYPE element to be ignored. No DTD processing occurs
  Parse // Used for parsing DTDs.
}

 

4. Schemas Property

“System.Xml.Schema.XmlSchemaSet” is a list of “Schema” objects

  • public XmlSchemaSet Schemas { get; set; }
XmlReaderSettings mySettings = new XmlReaderSettings();
mySettings.Schemas.Add("http://www.my.com/books", "books.xsd");

 

5. ValidationFlags Property

It is used when the “ValidationType” is “Schema“.

  • public XmlSchemaValidationFlags ValidationFlags { get; set; }
public enum XmlSchemaValidationFlags
{
  None, //do not process anything
  ProcessInlineSchema, // process inline schemas
  ProcessSchemaLocation, // process schema location hints
  ReportValidationWarnings, // report schema validation warnings
  ProcessIdentityConstraints, // process identity constraints (xs:ID, xs:IDREF, xs:key, xs:keyref, xs:unique)
  AllowXmlAttributes // allow xml:* attributes even if they are not defined in the schema. The attributes will be validated based on their data type.
}

ProcessIdentityConstraints” and “AllowXmlAttributes” are enabled by default.

 

6. Well-formedness Errors and Validation Errors

  • An “XmlException” is thrown when a well-formedness error is encountered.
  • A “ValidationEventHandler” is fired when a validation error is encountered.

 

7. ValidationEventHandler

XmlReaderSettings” has the “ValidationEventHandler” event. To catch the validation errors, you need to add the handler to this event.

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

static void ValidationCallBack (object sender, ValidationEventArgs args) { }

 

8. ValidationEventArgs

The “System.Xml.Schema.ValidationEventArgs” returns detailed information related to the “ValidationEventHandler“.

It has the following properties:

  • public string Message { get; }
  • public XmlSeverityType Severity { get; }

The “XmlSeverityType” enum has two values:

  • Error: indicates a validation error occurred when validating the instance document. If no validation event handler has been created, errors throw an exception.
  • Warning: indicates that a validation event occurred that is not an error. A warning is typically issued when there is no DTD, or XML Schema to validate a particular element or attribute against. Unlike errors, warnings do not throw an exception if there is no validation event handler.
static void ValidationCallBack(object sender, ValidationEventArgs args)
{
  if (args.Severity == XmlSeverityType.Warning)
    Console.WriteLine("Warning: Matching schema not found.  No validation occurred." + args.Message);
  else // Error
   Console.WriteLine("Validation error: " + args.Message);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s