XmlSchemaCollection.ValidationEventHandler Event

Use BaseTrue

Sets an event handler for receiving information about the XDR and XML schema validation errors.

Namespace:   System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public event ValidationEventHandler ValidationEventHandler

These events occur when the schemas are added to the collection. If an event handler is not provided, an XmlSchemaException is thrown on any validation errors where the Severity is XmlSeverityType.Error. To specify an event handler, define a callback function and add it to the ValidationEventHandler.


The XmlSchemaCollection class is obsolete in the .NET Framework version 2.0 and has been replaced by the XmlSchemaSet class.

The following example shows how to set an event handler to handle invalid XML Schemas.

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample

  public static void Main (){

    //Create the schema collection.
    XmlSchemaCollection xsc = new XmlSchemaCollection();

    //Set an event handler to manage invalid schemas.
    xsc.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

    //Add the schema to the collection.  
    xsc.Add(null, "invalid.xsd");


  //Display the schema error information.
  private static void ValidationCallBack (object sender, ValidationEventArgs args){
     Console.WriteLine("Invalid XSD schema: " + args.Exception.Message);

The preceding example uses the file invalid.xsd as input.

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' >
 <xsd:complexType name="personName">
            <xsd:element name="title" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="forename" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="surname"/>

   <xsd:complexType name="simpleName">
            <xsd:restriction base="personName">
                    <xsd:element name="title" minOccurs="0" maxOccurs="0"/>
                    <xsd:element name="firstname" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="surname"/>


.NET Framework
Available since 1.1
Return to top