XML Schema (XSD) Validation with XmlSchemaCollection
You can use the XmlSchemaCollection to validate an XML document against XML Schema definition language (XSD) schemas. The XmlSchemaCollection improves performance by storing schemas in the collection so they are not loaded into memory each time validation occurs. If the schema exists in the schema collection, the schemaLocation attribute is used to look up the schema in the collection.
The following example shows the root element of a data file.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema"
elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">
For this example, the value of the targetNamespace attribute is urn:bookstore-schema, which is the same namespace that is used when adding the schema to the XmlSchemaCollection.
The following code example adds an XML Schema to the XmlSchemaCollection.
Dim xsc As New XmlSchemaCollection()
' XML Schema.
xsc.Add("urn:bookstore-schema", schema)
reader = New XmlTextReader(filename)
vreader = New XmlValidatingReader(reader)
vreader.Schemas.Add(xsc)
[C#]
XmlSchemaCollection xsc = new XmlSchemaCollection();
// XML Schema.
xsc.Add("urn:bookstore-schema", schema);
reader = new XmlTextReader (filename);
vreader = new XmlValidatingReader (reader);
vreader.Schemas.Add(xsc);
The targetNamespace attribute is generally used when you add the namespaceURI property in the Add method for the XmlSchemaCollection. You can specify a null reference before adding the schema to the XmlSchemaCollection. An empty string ("") should be used for schemas without a namespace. The XmlSchemaCollection can have only one schema without a namespace.
The following code example adds an XML Schema, HeadCount.xsd, to the XmlSchemaCollection and validates HeadCount.xml.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Namespace ValidationSample
Class Sample
Public Shared Sub Main()
Dim tr As New XmlTextReader("HeadCount.xml")
Dim vr As New XmlValidatingReader(tr)
vr.Schemas.Add("xsdHeadCount", "HeadCount.xsd")
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
While vr.Read()
End While
Console.WriteLine("Validation finished")
End Sub
' Main
Public Shared Sub ValidationHandler(sender As Object, args As ValidationEventArgs)
Console.WriteLine("***Validation error")
Console.WriteLine("Severity:{0}", args.Severity)
Console.WriteLine("Message:{0}", args.Message)
End Sub
' ValidationHandler
End Class
' Sample
End Namespace
' ValidationSample
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace ValidationSample
{
class Sample
{
public static void Main()
{
XmlTextReader tr = new XmlTextReader("HeadCount.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.Schemas.Add("xsdHeadCount", "HeadCount.xsd");
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
while(vr.Read());
Console.WriteLine("Validation finished");
}
public static void ValidationHandler(object sender, ValidationEventArgs args)
{
Console.WriteLine("***Validation error");
Console.WriteLine("\tSeverity:{0}", args.Severity);
Console.WriteLine("\tMessage :{0}", args.Message);
}
}
}
The following outlines the contents of the input file, HeadCount.xml, to be validated.
<!--Load HeadCount.xsd in SchemaCollection for Validation-->
<hc:HeadCount xmlns:hc='xsdHeadCount'>
<Name>Waldo Pepper</Name>
<Name>Red Pepper</Name>
</hc:HeadCount>
The following outlines the contents of the XML Schema file, HeadCount.xsd, to be validated against.
<xs:schema xmlns="xsdHeadCount" targetNamespace="xsdHeadCount" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='HeadCount' type="HEADCOUNT"/>
<xs:complexType name="HEADCOUNT">
<xs:sequence>
<xs:element name='Name' type='xs:string' maxOccurs='unbounded'/>
</xs:sequence>
<xs:attribute name='division' type='xs:int' use='optional' default='8'/>
</xs:complexType>
</xs:schema>
The following code example creates an XmlValidatingReader that takes an XmlTextReader. The input file, sample4.xml, is validated against the XML Schema, sample4.xsd.
Dim tr As New XmlTextReader("sample4.xml")
Dim vr As New XmlValidatingReader(tr)
vr.ValidationType = ValidationType.Schema
vr.Schemas.Add("datatypesTest", "sample4.xsd")
AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack
While vr.Read()
Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name)
End While
[C#]
XmlTextReader tr = new XmlTextReader("sample4.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.ValidationType = ValidationType.Schema;
vr.Schemas.Add("datatypesTest", "sample4.xsd");
vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
while(vr.Read()) {
Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name);
}
The following outlines the contents of the input file, sample4.xml, to be validated.
<datatypes xmlns="datatypesTest">
<number>
<number_1>123</number_1>
</number>
</datatypes>
The following outlines the contents of the XML Schema file, sample4.xsd, to be validated against.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="datatypesTest"
targetNamespace="datatypesTest"
elementFormDefault="qualified">
<xs:element name = "datatypes">
<xs:complexType>
<xs:all>
<xs:element name="number">
<xs:complexType>
<xs:sequence>
<xs:element name="number_1" type="xs:decimal" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
See Also
XmlSchemaCollection as a Schema Cache | Validation of XML with Schemas | XmlParserContext Class | XmlValidatingReader.ValidationEventHandler Event | XmlValidatingReader.Schemas Property