Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

XmlReaderSettings.ValidationEventHandler Event

Note: This event is new in the .NET Framework version 2.0.

Occurs when the reader encounters validation errors.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public event ValidationEventHandler ValidationEventHandler
/** @event */
public void add_ValidationEventHandler (ValidationEventHandler value)

/** @event */
public void remove_ValidationEventHandler (ValidationEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

These events occur while reading an XML instance document if the ValidationType is set to either DTD or Schema. If the ReportValidationWarnings setting has been enabled on the ValidationFlags property these events also occur when any validation warnings are encountered.

If the reader is configured for validation and no validation event handler has been set, an XmlSchemaValidationException is thrown for all validation errors. (Validation warnings do not cause an XmlSchemaValidationException to be thrown).

Security noteSecurity Note

Validation error messages may expose sensitive content model information. Validation error and warning messages are handled using the ValidationEventHandler delegate, or are exposed as an XmlSchemaValidationException if no event handler is provided to the XmlReaderSettings object (validation warnings do not cause an XmlSchemaValidationException to be thrown). This content model information should not be exposed in untrusted scenarios. Validation warning messages are suppressed by default and can be reported by setting the ReportValidationWarnings flag.

The SourceUri property of an XmlSchemaValidationException returns the URI path to the schema file that caused the exception. The SourceUri property should not be exposed in untrusted scenarios.

The following example shows the settings to specify to create a reader that validates using an inline schema and that also displays validation warnings. The validation event handler uses the XmlSeverityType enumeration to differentiate between warnings and errors.

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);

  }  

}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.