XmlSchema XmlSchema XmlSchema XmlSchema Class

定义

一个 XML 架构的内存表示形式,如万维网联合会 (W3C) XML 架构第 1 部分:结构XML 架构第 2 部分:数据类型]中所述。An in-memory representation of an XML Schema, as specified in the World Wide Web Consortium (W3C) XML Schema Part 1: Structures and XML Schema Part 2: Datatypes].

public ref class XmlSchema : System::Xml::Schema::XmlSchemaObject
public class XmlSchema : System.Xml.Schema.XmlSchemaObject
type XmlSchema = class
    inherit XmlSchemaObject
Public Class XmlSchema
Inherits XmlSchemaObject
继承

示例

下面的示例创建一个架构定义。The following example creates a schema definition.

#using <mscorlib.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

class XmlSchemaExamples
{
public:
    static void Main()
    {

        XmlSchema^ schema = gcnew XmlSchema();

        // <xs:element name="cat" type="xs:string"/>
        XmlSchemaElement^ elementCat = gcnew XmlSchemaElement();
        schema->Items->Add(elementCat);
        elementCat->Name = "cat";
        elementCat->SchemaTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="dog" type="xs:string"/>
        XmlSchemaElement^ elementDog = gcnew XmlSchemaElement();
        schema->Items->Add(elementDog);
        elementDog->Name = "dog";
        elementDog->SchemaTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="redDog" substitutionGroup="dog" />
        XmlSchemaElement^ elementRedDog = gcnew XmlSchemaElement();
        schema->Items->Add(elementRedDog);
        elementRedDog->Name = "redDog";
        elementRedDog->SubstitutionGroup = gcnew XmlQualifiedName("dog");

        // <xs:element name="brownDog" substitutionGroup ="dog" />
        XmlSchemaElement^ elementBrownDog = gcnew XmlSchemaElement();
        schema->Items->Add(elementBrownDog);
        elementBrownDog->Name = "brownDog";
        elementBrownDog->SubstitutionGroup = gcnew XmlQualifiedName("dog");


        // <xs:element name="pets">
        XmlSchemaElement^ elementPets = gcnew XmlSchemaElement();
        schema->Items->Add(elementPets);
        elementPets->Name = "pets";

        // <xs:complexType>
        XmlSchemaComplexType^ complexType = gcnew XmlSchemaComplexType();
        elementPets->SchemaType = complexType;

        // <xs:choice minOccurs="0" maxOccurs="unbounded">
        XmlSchemaChoice^ choice = gcnew XmlSchemaChoice();
        complexType->Particle = choice;
        choice->MinOccurs = 0;
        choice->MaxOccursString = "unbounded";

        // <xs:element ref="cat"/>
        XmlSchemaElement^ catRef = gcnew XmlSchemaElement();
        choice->Items->Add(catRef);
        catRef->RefName = gcnew XmlQualifiedName("cat");

        // <xs:element ref="dog"/>
        XmlSchemaElement^ dogRef = gcnew XmlSchemaElement();
        choice->Items->Add(dogRef);
        dogRef->RefName = gcnew XmlQualifiedName("dog");

        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallbackOne);
        schemaSet->Add(schema);
        schemaSet->Compile();

        XmlSchema^ compiledSchema;

        for each (XmlSchema^ schema1 in schemaSet->Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager(gcnew NameTable());
        nsmgr->AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema->Write(Console::Out, nsmgr);
    }

    static void ValidationCallbackOne(Object^ sender, ValidationEventArgs^ args)
    {
        Console::WriteLine(args->Message);
    }
};

int main()
{
    XmlSchemaExamples::Main();
    return 0;
};
using System;
using System.Xml;
using System.Xml.Schema;

class XMLSchemaExamples
{
    public static void Main()
    {

        XmlSchema schema = new XmlSchema();

        // <xs:element name="cat" type="xs:string"/>
        XmlSchemaElement elementCat = new XmlSchemaElement();
        schema.Items.Add(elementCat);
        elementCat.Name = "cat";
        elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="dog" type="xs:string"/>
        XmlSchemaElement elementDog = new XmlSchemaElement();
        schema.Items.Add(elementDog);
        elementDog.Name = "dog";
        elementDog.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="redDog" substitutionGroup="dog" />
        XmlSchemaElement elementRedDog = new XmlSchemaElement();
        schema.Items.Add(elementRedDog);
        elementRedDog.Name = "redDog";
        elementRedDog.SubstitutionGroup = new XmlQualifiedName("dog");

        // <xs:element name="brownDog" substitutionGroup ="dog" />
        XmlSchemaElement elementBrownDog = new XmlSchemaElement();
        schema.Items.Add(elementBrownDog);
        elementBrownDog.Name = "brownDog";
        elementBrownDog.SubstitutionGroup = new XmlQualifiedName("dog");


        // <xs:element name="pets">
        XmlSchemaElement elementPets = new XmlSchemaElement();
        schema.Items.Add(elementPets);
        elementPets.Name = "pets";

        // <xs:complexType>
        XmlSchemaComplexType complexType = new XmlSchemaComplexType();
        elementPets.SchemaType = complexType;

        // <xs:choice minOccurs="0" maxOccurs="unbounded">
        XmlSchemaChoice choice = new XmlSchemaChoice();
        complexType.Particle = choice;
        choice.MinOccurs = 0;
        choice.MaxOccursString = "unbounded";

        // <xs:element ref="cat"/>
        XmlSchemaElement catRef = new XmlSchemaElement();
        choice.Items.Add(catRef);
        catRef.RefName = new XmlQualifiedName("cat");

        // <xs:element ref="dog"/>
        XmlSchemaElement dogRef = new XmlSchemaElement();
        choice.Items.Add(dogRef);
        dogRef.RefName = new XmlQualifiedName("dog");

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
        schemaSet.Add(schema);
        schemaSet.Compile();

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);
    }

    public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
    {
        Console.WriteLine(args.Message);
    }
}
Option Explicit On
Option Strict On

Imports System.Xml
Imports System.Xml.Schema

Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()

        ' <xs:element name="cat" type="xs:string"/>
        Dim elementCat As New XmlSchemaElement()
        schema.Items.Add(elementCat)
        elementCat.Name = "cat"
        elementCat.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

        ' <xs:element name="dog" type="xs:string"/>
        Dim elementDog As New XmlSchemaElement()
        schema.Items.Add(elementDog)
        elementDog.Name = "dog"
        elementDog.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

        ' <xs:element name="redDog" substitutionGroup="dog" />
        Dim elementRedDog As New XmlSchemaElement()
        schema.Items.Add(elementRedDog)
        elementRedDog.Name = "redDog"
        elementRedDog.SubstitutionGroup = New XmlQualifiedName("dog")

        ' <xs:element name="brownDog" substitutionGroup ="dog" />
        Dim elementBrownDog As New XmlSchemaElement()
        schema.Items.Add(elementBrownDog)
        elementBrownDog.Name = "brownDog"
        elementBrownDog.SubstitutionGroup = New XmlQualifiedName("dog")

        ' <xs:element name="pets">
        Dim elementPets As New XmlSchemaElement()
        schema.Items.Add(elementPets)
        elementPets.Name = "pets"

        ' <xs:complexType>
        Dim complexType As New XmlSchemaComplexType()
        elementPets.SchemaType = complexType

        ' <xs:choice minOccurs="0" maxOccurs="unbounded">
        Dim choice As New XmlSchemaChoice()
        complexType.Particle = choice
        choice.MinOccurs = 0
        choice.MaxOccursString = "unbounded"

        ' <xs:element ref="cat"/>
        Dim catRef As New XmlSchemaElement()
        choice.Items.Add(catRef)
        catRef.RefName = New XmlQualifiedName("cat")

        ' <xs:element ref="dog"/>
        Dim dogRef As New XmlSchemaElement()
        choice.Items.Add(dogRef)
        dogRef.RefName = New XmlQualifiedName("dog")

        Dim schemaSet As New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallbackOne

        schemaSet.Add(schema)
        schemaSet.Compile()

        Dim compiledSchema As XmlSchema = Nothing

        For Each schema1 As XmlSchema In schemaSet.Schemas()
            compiledSchema = schema1
        Next

        Dim nsmgr As New XmlNamespaceManager(New NameTable())
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
        compiledSchema.Write(Console.Out, nsmgr)


    End Sub


    Public Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Console.WriteLine(args.Message)
    End Sub
End Class

下面的 XML 文件是针对前面的代码示例生成的。The following XML file is generated for the preceding code example.


<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="cat" type="xs:string"/>
    <xs:element name="dog" type="xs:string"/>
    <xs:element name="redDog" type="xs:string" substitutionGroup="dog"/>
    <xs:element name="brownDog" type="xs:string" substitutionGroup ="dog" />

    <xs:element name="pets">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="cat"/>
          <xs:element ref="dog"/>
        </xs:choice>
      </xs:complexType>
    </xs:element>
</xs:schema>

注解

重要

  • 不要使用来自未知或不受信任的源或位置的架构。Do not use schemas from unknown or untrusted sources or locations. 这样做会破坏代码的安全性。Doing so will compromise the security of your code.
  • XML 架构(包括内联架构)本质上容易受到拒绝服务攻击;不要在不受信任的情况下接受它们。XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.
  • 由于使用XmlSchema类(如XmlSchemaException类)而引发的异常可能包含不应在不受信任的方案中公开的敏感信息。Exceptions raised as a result of using the XmlSchema class, such as the XmlSchemaException class, may contain sensitive information that should not be exposed in untrusted scenarios. 例如, SourceUri的属性XmlSchemaException将返回引发异常的架构文件的 URI 路径。For example, the SourceUri property of an XmlSchemaException returns the URI path to the schema file that caused the exception. SourceUri应在不受信任的方案中公开属性。The SourceUri property should not be exposed in untrusted scenarios. 异常应得到正确处理,因此不受信任的情况下不会公开此敏感信息。Exceptions should be properly handled so this sensitive information is not exposed in untrusted scenarios.

构造函数

XmlSchema() XmlSchema() XmlSchema() XmlSchema()

初始化 XmlSchema 类的新实例。Initializes a new instance of the XmlSchema class.

字段

InstanceNamespace InstanceNamespace InstanceNamespace InstanceNamespace

XML 架构实例命名空间。The XML schema instance namespace. 此字段为常数。This field is constant.

Namespace Namespace Namespace Namespace

XML 架构命名空间。The XML schema namespace. 此字段为常数。This field is constant.

属性

AttributeFormDefault AttributeFormDefault AttributeFormDefault AttributeFormDefault

获取或设置在架构的目标命名空间中声明的属性的窗体。Gets or sets the form for attributes declared in the target namespace of the schema.

AttributeGroups AttributeGroups AttributeGroups AttributeGroups

获取架构中所有全局属性组的架构编译后值。Gets the post-schema-compilation value of all the global attribute groups in the schema.

Attributes Attributes Attributes Attributes

获取架构中所有属性的架构编译后值。Gets the post-schema-compilation value for all the attributes in the schema.

BlockDefault BlockDefault BlockDefault BlockDefault

获取或设置 blockDefault 特性,该特性设置 block 架构中的元素和复杂类型的 targetNamespace 特性的默认值。Gets or sets the blockDefault attribute which sets the default value of the block attribute on element and complex types in the targetNamespace of the schema.

ElementFormDefault ElementFormDefault ElementFormDefault ElementFormDefault

获取或设置在架构的目标命名空间中声明的元素的窗体。Gets or sets the form for elements declared in the target namespace of the schema.

Elements Elements Elements Elements

获取架构中所有元素的架构编译后值。Gets the post-schema-compilation value for all the elements in the schema.

FinalDefault FinalDefault FinalDefault FinalDefault

获取或设置 finalDefault 特性,该特性设置架构目标命名空间中的元素和复杂类型的 final 特性的默认值。Gets or sets the finalDefault attribute which sets the default value of the final attribute on elements and complex types in the target namespace of the schema.

Groups Groups Groups Groups

获取架构中所有组的架构编译后值。Gets the post-schema-compilation value of all the groups in the schema.

Id Id Id Id

获取或设置字符串 ID。Gets or sets the string ID.

Includes Includes Includes Includes

获取包含的和导入的架构的集合。Gets the collection of included and imported schemas.

IsCompiled IsCompiled IsCompiled IsCompiled

表明架构是否已编译。Indicates if the schema has been compiled.

Items Items Items Items

获取架构中架构元素的集合,并在 schema 元素级别用于添加新的元素类型。Gets the collection of schema elements in the schema and is used to add new element types at the schema element level.

LineNumber LineNumber LineNumber LineNumber

获取或设置 schema 元素引用的文件中的行号。Gets or sets the line number in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
LinePosition LinePosition LinePosition LinePosition

获取或设置 schema 元素引用的文件中的行位置。Gets or sets the line position in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
Namespaces Namespaces Namespaces Namespaces

获取或设置用于此架构对象的 XmlSerializerNamespacesGets or sets the XmlSerializerNamespaces to use with this schema object.

(Inherited from XmlSchemaObject)
Notations Notations Notations Notations

获取架构中所有注释的架构编译后值。Gets the post-schema-compilation value for all notations in the schema.

Parent Parent Parent Parent

获取或设置此 XmlSchemaObject 的父级。Gets or sets the parent of this XmlSchemaObject.

(Inherited from XmlSchemaObject)
SchemaTypes SchemaTypes SchemaTypes SchemaTypes

获取架构中所有架构类型的架构编译后值。Gets the post-schema-compilation value of all schema types in the schema.

SourceUri SourceUri SourceUri SourceUri

获取或设置加载了架构的文件的源位置。Gets or sets the source location for the file that loaded the schema.

(Inherited from XmlSchemaObject)
TargetNamespace TargetNamespace TargetNamespace TargetNamespace

获取或设置架构目标命名空间的统一资源标识符 (URI)。Gets or sets the Uniform Resource Identifier (URI) of the schema target namespace.

UnhandledAttributes UnhandledAttributes UnhandledAttributes UnhandledAttributes

获取或设置不属于架构目标命名空间的限定属性。Gets or sets the qualified attributes which do not belong to the schema target namespace.

Version Version Version Version

获取或设置架构的版本。Gets or sets the version of the schema.

方法

Compile(ValidationEventHandler) Compile(ValidationEventHandler) Compile(ValidationEventHandler) Compile(ValidationEventHandler)

将 XML 架构对象模型 (SOM) 编译为架构信息供验证使用。Compiles the XML Schema Object Model (SOM) into schema information for validation. 用于检查以编程方式生成的 SOM 的语法和语义结构。Used to check the syntactic and semantic structure of the programmatically built SOM. 语义验证检查在编译期间执行。Semantic validation checking is performed during compilation.

Compile(ValidationEventHandler, XmlResolver) Compile(ValidationEventHandler, XmlResolver) Compile(ValidationEventHandler, XmlResolver) Compile(ValidationEventHandler, XmlResolver)

将 XML 架构对象模型 (SOM) 编译为架构信息供验证使用。Compiles the XML Schema Object Model (SOM) into schema information for validation. 用于检查以编程方式生成的 SOM 的语法和语义结构。Used to check the syntactic and semantic structure of the programmatically built SOM. 语义验证检查在编译期间执行。Semantic validation checking is performed during compilation.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Read(Stream, ValidationEventHandler) Read(Stream, ValidationEventHandler) Read(Stream, ValidationEventHandler) Read(Stream, ValidationEventHandler)

从提供的流中读取 XML 架构。Reads an XML Schema from the supplied stream.

Read(TextReader, ValidationEventHandler) Read(TextReader, ValidationEventHandler) Read(TextReader, ValidationEventHandler) Read(TextReader, ValidationEventHandler)

从提供的 TextReader 读取 XML 架构。Reads an XML Schema from the supplied TextReader.

Read(XmlReader, ValidationEventHandler) Read(XmlReader, ValidationEventHandler) Read(XmlReader, ValidationEventHandler) Read(XmlReader, ValidationEventHandler)

从提供的 XmlReader 读取 XML 架构。Reads an XML Schema from the supplied XmlReader.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
Write(Stream) Write(Stream) Write(Stream) Write(Stream)

将 XML 架构写入提供的数据流中。Writes the XML Schema to the supplied data stream.

Write(Stream, XmlNamespaceManager) Write(Stream, XmlNamespaceManager) Write(Stream, XmlNamespaceManager) Write(Stream, XmlNamespaceManager)

使用指定的 Stream 将“XML 架构”写入提供的 XmlNamespaceManagerWrites the XML Schema to the supplied Stream using the XmlNamespaceManager specified.

Write(TextWriter) Write(TextWriter) Write(TextWriter) Write(TextWriter)

将“XML 架构”写入提供的 TextWriterWrites the XML Schema to the supplied TextWriter.

Write(TextWriter, XmlNamespaceManager) Write(TextWriter, XmlNamespaceManager) Write(TextWriter, XmlNamespaceManager) Write(TextWriter, XmlNamespaceManager)

将“XML 架构”写入提供的 TextWriterWrites the XML Schema to the supplied TextWriter.

Write(XmlWriter) Write(XmlWriter) Write(XmlWriter) Write(XmlWriter)

将“XML 架构”写入提供的 XmlWriterWrites the XML Schema to the supplied XmlWriter.

Write(XmlWriter, XmlNamespaceManager) Write(XmlWriter, XmlNamespaceManager) Write(XmlWriter, XmlNamespaceManager) Write(XmlWriter, XmlNamespaceManager)

将“XML 架构”写入提供的 XmlWriterWrites the XML Schema to the supplied XmlWriter.

适用于

另请参阅