[C# – XML] Validation Using an Inline XML Schema with XmlReader

In some cases, XML Schema definitions are embedded in the XML document. It is called “Inline XML Schema”. If an Inline XML Schema is used, you cannot use “XmlSchemaSet” anymore and need to change the “XmlReaderSettings.ValidationFlags” property to enable the “ProcessInlineSchema” option.

 

1.  ValidationFlags Property

XmlReaderSettings” has the “ValidationFlags” property.

  • 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
  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.

In order to use an Inline XML Schema, the “ProcessInlineSchema” should be enabled. Usually, it is a good idea to enable the “ReportValidationWarnings” property too.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

 

2.  XmlReaderSettings Object

  • Create an “XmlReaderSettings” object
  • Set the “ValidationType” property to “Schema
  • Change the “ValidationFlags” property

 

3. Validation Event Handler

  • Create a validation event handler method with 2 parameters: (object sender, ValidationEventArgs args)
  • Add the event handler to the “ValidationEventHandler” event of the “XmlReaderSettings” object

 

4. XmlReader

  • Create an “XmlReader” object using the static “Create” method

 

5. XML/Inline Schema Sample

<?xml version="1.0" ?>
<books>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="www.book.com"
  xmlns="www.book.com">
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string" />
        <xs:element name="author" type="xs:string" />
      xs:sequence>
      <xs:attribute name="isbn" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>
  <b:book  isbn="157281055"
    xmlns:b="www.book.com">
    <title>The Lord of the Rings</title>
    <author>J.R.R. Tolkien</author>
  </b:book>
</books>

 

6. C# Sample Code

public static void ReadXMLwithInlineXSD()
{
  // Set the validation settings.
  XmlReaderSettings settings = new XmlReaderSettings();
  settings.ValidationType = ValidationType.Schema;
  settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
  settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
  settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
  settings.IgnoreWhitespace = true;

  // Create the XmlReader object.
  XmlReader reader = XmlReader.Create("bookInlineSchema.xml", settings);

  // Parse the file.
  while (reader.Read())
  {
    System.Console.WriteLine("{0}, {1}: {2} ", reader.NodeType, reader.Name, reader.Value);
  }
}

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
  if (e.Severity == XmlSeverityType.Warning)
    Console.WriteLine("Warning: Matching schema not found.  No validation occurred." + e.Message);
  else // Error
    Console.WriteLine("Validation error: " + e.Message);
}

Feel free to change XML and look at what kind of errors you get.

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