XmlReaderSettings.ValidationEventHandler 事件

定义

在读取器遇到验证错误时发生。Occurs when the reader encounters validation errors.

public:
 event System::Xml::Schema::ValidationEventHandler ^ ValidationEventHandler;
public event System.Xml.Schema.ValidationEventHandler ValidationEventHandler;
member this.ValidationEventHandler : System.Xml.Schema.ValidationEventHandler 

事件类型

ValidationEventHandler

示例

下面的示例演示了用于创建使用内联架构进行验证并且也显示验证警告的读取器的设置。The following example shows the settings to specify to create a reader that validates using an inline schema and that also displays validation warnings. 验证事件处理程序使用 XmlSeverityType 枚举来区分警告和错误。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);
  }
}
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 

注解

如果将设置为或,则在读取 XML 实例文档时,将发生这些事件 ValidationType DTD SchemaThese events occur while reading an XML instance document if the ValidationType is set to either DTD or Schema. 如果对属性启用了该设置,则在 ReportValidationWarnings ValidationFlags 遇到任何验证警告时也会发生这些事件。If the ReportValidationWarnings setting has been enabled on the ValidationFlags property these events also occur when any validation warnings are encountered.

如果为验证配置了读取器,但未设置任何验证事件处理程序, XmlSchemaValidationException 则将为所有验证错误引发。If the reader is configured for validation and no validation event handler has been set, an XmlSchemaValidationException is thrown for all validation errors. (验证警告不会导致 XmlSchemaValidationException) 引发。(Validation warnings do not cause an XmlSchemaValidationException to be thrown).

重要

验证错误消息可能会暴露敏感内容模型信息。Validation error messages may expose sensitive content model information. 使用委托处理验证错误和警告消息 ValidationEventHandlerXmlSchemaValidationException 如果不向对象提供任何事件处理程序 XmlReaderSettings (验证警告不会导致) 引发,则将使用委托来处理验证错误和警告消息 XmlSchemaValidationExceptionValidation 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. 默认情况下,会禁止显示验证警告消息,并可通过设置标志来报告验证警告消息 ReportValidationWarningsValidation warning messages are suppressed by default and can be reported by setting the ReportValidationWarnings flag.

SourceUri 属性将 XmlSchemaValidationException 返回引发异常的架构文件的 URI 路径。The SourceUri property of an XmlSchemaValidationException returns the URI path to the schema file that caused the exception. SourceUri不应在不受信任的方案中公开属性。The SourceUri property should not be exposed in untrusted scenarios.

适用于

另请参阅