Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


XmlReaderSettings.ValidationFlags Property

Definition

Gets or sets a value indicating the schema validation settings. This setting applies to XmlReader objects that validate schemas (ValidationType property set to ValidationType.Schema).

C#
public System.Xml.Schema.XmlSchemaValidationFlags ValidationFlags { get; set; }

Property Value

A bitwise combination of enumeration values that specify validation options. ProcessIdentityConstraints and AllowXmlAttributes are enabled by default. ProcessInlineSchema, ProcessSchemaLocation, and ReportValidationWarnings are disabled by default.

Examples

The following example validates an XML file against an inline XML Schema by enabling the ProcessInlineSchema setting. The XML reader is configured to display validation warnings, so you also see the expected warning on the root element.

C#
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);
  }
}

The example uses the inlineSchema.xml file as input.

XML
<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>

The output is as follows:

Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'root'.

Validation error: The element 'xsdHeadCount:HeadCount' has invalid child element 'division'. Expected 'ID'.

Remarks

Fontos

The ProcessInlineSchema and ProcessSchemaLocation validation flags of an XmlReaderSettings object are not set by default. 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. If 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.

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

Applies to

Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also