XmlReaderSettings.ValidationFlags XmlReaderSettings.ValidationFlags XmlReaderSettings.ValidationFlags XmlReaderSettings.ValidationFlags Property


获取或设置一个指示架构验证设置的值。Gets or sets a value indicating the schema validation settings. 此设置应用于验证架构的 XmlReader 对象(ValidationType 属性设置为 ValidationType.Schema)。This setting applies to XmlReader objects that validate schemas (ValidationType property set to ValidationType.Schema).

 property System::Xml::Schema::XmlSchemaValidationFlags ValidationFlags { System::Xml::Schema::XmlSchemaValidationFlags get(); void set(System::Xml::Schema::XmlSchemaValidationFlags value); };
public System.Xml.Schema.XmlSchemaValidationFlags ValidationFlags { get; set; }
member this.ValidationFlags : System.Xml.Schema.XmlSchemaValidationFlags with get, set
Public Property ValidationFlags As XmlSchemaValidationFlags


指定验证选项的枚举值的按位组合。A bitwise combination of enumeration values that specify validation options. ProcessIdentityConstraintsAllowXmlAttributes 默认情况下启用。ProcessIdentityConstraints and AllowXmlAttributes are enabled by default. ProcessInlineSchemaProcessSchemaLocationReportValidationWarnings 默认情况下禁用。ProcessInlineSchema, ProcessSchemaLocation, and ReportValidationWarnings are disabled by default.


下面的示例通过启用此ProcessInlineSchema设置, 根据内联 xml 架构验证 XML 文件。The following example validates an XML file against an inline XML Schema by enabling the ProcessInlineSchema setting. XML 读取器配置为显示验证警告, sp 你还会在根元素上看到预期的警告。The XML reader is configured to display validation warnings, sp you also see the expected warning on the root element.

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);
        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)
        Console.WriteLine("   Validation error: " + args.Message)
     end if
  end sub 

end class 


示例使用 inlineSchema.xml 文件作为输入。The example uses the inlineSchema.xml file as input.

<!--Start of schema-->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
    <xs:element name='HeadCount'>
                <xs:element name='ID' type='xs:unsignedShort' maxOccurs='unbounded' />
            <xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
<!--End of schema-->
<hc:HeadCount xmlns:hc='xsdHeadCount'>


警告: 找不到匹配的架构。Warning: Matching schema not found. 未进行任何验证。No validation occurred. 找不到元素“root”的架构信息。Could not find schema information for the element 'root'.

验证错误: 元素 ' xsdHeadCount: 人员人数 ' 具有无效的子元素 ' 除法 '。Validation error: The element 'xsdHeadCount:HeadCount' has invalid child element 'division'. 应包含“ID”。Expected 'ID'.



默认情况下不设置 ProcessInlineSchema 对象的 ProcessSchemaLocationXmlReaderSettings 验证标志。The ProcessInlineSchema and ProcessSchemaLocation validation flags of an XmlReaderSettings object are not set by default. 设置了这些标志后,XmlResolver 对象的 XmlReaderSettings 用于在 XmlReader 中解析实例文档中遇到的架构位置。When these flags are set, the XmlResolver of the XmlReaderSettings object is used to resolve schema locations encountered in the instance document in the XmlReader. 如果对象为null, 则即使设置了ProcessInlineSchemaProcessSchemaLocation验证标志, 也不会解析架构位置。 XmlResolverIf the XmlResolver object is null, schema locations are not resolved even if the ProcessInlineSchema and ProcessSchemaLocation validation flags are set.

在验证过程中添加的架构会添加新类型,并且可能更改被验证的文档的验证结果。Schemas added during validation add new types and can change the validation outcome of the document being validated. 因此,只应从可信的源解析外部架构。As a result, external schemas should only be resolved from trusted sources.

如果在高可用性方案中针对包含对文档大部分的标识约束的架构验证、不受信任的大型 XML 文档, 则建议禁用标志(默认启用)。ProcessIdentityConstraintsDisabling the ProcessIdentityConstraints flag (enabled by default) is recommended when validating, untrusted, large XML documents in high availability scenarios against a schema with identity constraints over a large part of the document.