Validation Using an Inline XML Schema with XmlReader
You can validate an XML file against an inline XML Schema definition language (XSD) schema. Before creating the XmlReader, change the XmlReaderSettings.ValidationFlags property to enable the ProcessInlineSchema option.
Note
Because the inline schema appears as a child element of the root element, the root element cannot be validated when inline schema validation is performed. A validation warning is generated for the root element.
Example
The following example validates an XML file using an inline XML Schema. Because the reader is configured to display validation warnings, you also see the expected warning on the root element.
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
public class ValidXSD
public shared sub Main()
' Set the validation settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ProcessInlineSchema
settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("inlineSchema.xml", settings)
' Parse the file.
while (reader.Read())
end while
end sub
' Display any warnings or errors.
private shared sub ValidationCallBack (sender as object, args as ValidationEventArgs)
if (args.Severity=XmlSeverityType.Warning)
Console.WriteLine(" Warning: Matching schema not found. No validation occurred." + args.Message)
else
Console.WriteLine(" Validation error: " + args.Message)
end if
end sub
end class
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class ValidXSD {
public static void Main() {
// 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);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any warnings or errors.
private static void ValidationCallBack (object sender, ValidationEventArgs args) {
if (args.Severity==XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
}
// Set the validation settings.
XmlReaderSettings* settings = new XmlReaderSettings();
settings->XsdValidate = true;
settings->IgnoreInlineSchema = false;
settings->IgnoreValidationWarnings = false;
settings->ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
Input
The example uses the inlineSchema.xml file as input.
<root>
<!--Start of schema-->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns='xsdHeadCount'
targetNamespace='xsdHeadCount'>
<xs:element name='HeadCount'>
<xs:complexType>
<xs:sequence>
<xs:element name='ID' type='xs:unsignedShort' maxOccurs='unbounded' />
</xs:sequence>
<xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
</xs:complexType>
</xs:element>
</xs:schema>
<!--End of schema-->
<hc:HeadCount xmlns:hc='xsdHeadCount'>
<ID>12365</ID>
<ID>43295</ID>
<division>Accounting</division>
</hc:HeadCount>
</root>
Output
Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'root'.
Validation error: The element 'xsdHeadCount:HeadCount' has invalid child element 'division'. Expected 'ID'.
See Also
Concepts
Reading XML with the XmlReader