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

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

输入Input

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

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

OutputOutput

警告: 找不到匹配的架构。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.

适用于

另请参阅