Export (0) Print
Expand All

XmlSchemaCollection.ValidationEventHandler Event

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.

Important noteImportant

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft