Export (0) Print
Expand All

XmlReaderSettings.ValidationEventHandler Event

Occurs when the reader encounters validation errors.

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

'Declaration
Public Event ValidationEventHandler As ValidationEventHandler

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.

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

public class ValidXSD 

  public shared sub Main() 

    ' Set the validation settings. 
    Dim settings as XmlReaderSettings = new XmlReaderSettings()
    settings.ValidationType = ValidationType.Schema
    settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ProcessInlineSchema
    settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
      AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack

    ' Create the XmlReader object. 
    Dim reader as XmlReader = XmlReader.Create("inlineSchema.xml", settings)

    ' Parse the file. 
    while (reader.Read())
    end while
  end sub

  ' Display any warnings or errors.
  private shared sub ValidationCallBack (sender as object, args as ValidationEventArgs)
     if (args.Severity=XmlSeverityType.Warning)
       Console.WriteLine("   Warning: Matching schema not found.  No validation occurred." + args.Message)
     else
        Console.WriteLine("   Validation error: " + args.Message)
     end if
  end sub 

end class 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft