XmlSchemaValidationEventHandler Delegate

Namespace

System.Xml

Assembly

System.Xml.dll

Syntax

<delegate> public delegate void XmlSchemaValidationEventHandler(
    object sender,
    XmlSchemaValidationEventArgs e
);

Remarks

The XmlSchemaValidationEventHandler delegate is used to handle events that occur during XML schema validation. When an XML document is validated against a schema, the XmlSchemaValidationEventHandler delegate is invoked for each validation error or warning encountered.

You create an instance of the delegate by registering a method that accepts two parameters: an object representing the sender and an XmlSchemaValidationEventArgs object containing details about the validation event.

The XmlSchemaValidationEventArgs object provides information such as the severity of the event (error or warning), the line and position numbers where the event occurred, and a descriptive message.

Example

The following example shows how to register an event handler for schema validation.

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

public class XmlValidator {

    public static void Main(string[] args) {

        // Create a schema set
        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add("urn:example-schema", "example.xsd");

        // Create an XmlReaderSettings object and add the schema set
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas = schemas;

        // Set the validation type
        settings.ValidationType = ValidationType.Schema;

        // Add the event handler
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create an XmlReader
        using (XmlReader reader = XmlReader.Create("document.xml", settings)) {
            // Parse the file. Exceptions are caught by the ValidationCallBack.
            while (reader.Read()) { }
        }
    }

    // The validation event handler
    public static void ValidationCallBack(
        object sender,
        ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning) {
            Console.WriteLine("Warning: {0}", args.Message);
        }
        else if (args.Severity == XmlSeverityType.Error) {
            Console.WriteLine("Error: {0}", args.Message);
        }
    }
}

Members

This delegate does not define any members.

Requirements

Requirement Details
Client Windows 8, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008, Windows Server 2003 SP1 or later
Server Windows Server 2012, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003 SP1 or later
.NET Framework Class Library Supported in: 4.5, 4.0, 3.5, 3.0, 2.0
Namespace System.Xml
Assembly System.Xml.dll