通过


XmlSchema 类

定义

万维网联盟(W3C) XML 架构第 1 部分:结构和XML 架构第 2 部分:数据类型]中指定的 XML 架构的内存中表示形式。

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

示例

以下示例创建架构定义。

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 文件。


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

注解

重要

  • 请勿使用来自未知或不受信任的源或位置的架构。 这样做会损害代码的安全性。
  • XML 架构(包括内联架构)本质上容易受到拒绝服务攻击;在不受信任的方案中不接受它们。
  • 使用 XmlSchema 类(如 XmlSchemaException 类)引发的异常可能包含不应在不受信任的方案中公开的敏感信息。 例如,属性SourceUriXmlSchemaException返回导致异常的架构文件的 URI 路径。 SourceUri属性不应在不受信任的方案中公开。 应正确处理异常,以便在不受信任的方案中不公开此敏感信息。

构造函数

名称 说明
XmlSchema()

初始化 XmlSchema 类的新实例。

字段

名称 说明
InstanceNamespace

XML 架构实例命名空间。 此字段为常量。

Namespace

XML 架构命名空间。 此字段为常量。

属性

名称 说明
AttributeFormDefault

获取或设置架构的目标命名空间中声明的属性的窗体。

AttributeGroups

获取架构中所有全局属性组的架构编译后值。

Attributes

获取架构中的所有属性的架构编译后值。

BlockDefault

获取或设置特性blockDefault,该属性设置架构中targetNamespace元素和复杂类型的属性的默认值block

ElementFormDefault

获取或设置架构的目标命名空间中声明的元素的窗体。

Elements

获取架构中的所有元素的架构编译后值。

FinalDefault

获取或设置特性 finalDefault ,该属性设置架构的目标命名空间中元素和复杂类型的属性的默认值 final

Groups

获取架构中所有组的架构编译后值。

Id

获取或设置字符串 ID。

Includes

获取包含和导入的架构的集合。

IsCompiled

指示架构是否已编译。

Items

获取架构中的架构元素的集合,并用于在 schema 元素级别添加新元素类型。

LineNumber

获取或设置元素引用的文件 schema 中的行号。

(继承自 XmlSchemaObject)
LinePosition

获取或设置元素引用的文件中 schema 的行位置。

(继承自 XmlSchemaObject)
Namespaces

获取或设置 XmlSerializerNamespaces 要用于此架构对象。

(继承自 XmlSchemaObject)
Notations

获取架构中的所有表示法的架构编译后值。

Parent

获取或设置此 XmlSchemaObject对象的父级。

(继承自 XmlSchemaObject)
SchemaTypes

获取架构中所有架构类型的架构编译后值。

SourceUri

获取或设置加载架构的文件的源位置。

(继承自 XmlSchemaObject)
TargetNamespace

获取或设置架构目标命名空间的统一资源标识符(URI)。

UnhandledAttributes

获取或设置不属于架构目标命名空间的限定属性。

Version

获取或设置架构的版本。

方法

名称 说明
Compile(ValidationEventHandler, XmlResolver)
已过时.
已过时.
已过时.

将 XML 架构对象模型(SOM)编译为架构信息进行验证。 用于检查以编程方式生成的 SOM 的语法和语义结构。 在编译期间执行语义验证检查。

Compile(ValidationEventHandler)
已过时.
已过时.
已过时.

将 XML 架构对象模型(SOM)编译为架构信息进行验证。 用于检查以编程方式生成的 SOM 的语法和语义结构。 在编译期间执行语义验证检查。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Read(Stream, ValidationEventHandler)

从提供的流中读取 XML 架构。

Read(TextReader, ValidationEventHandler)

读取提供的 TextReaderXML 架构。

Read(XmlReader, ValidationEventHandler)

读取提供的 XmlReaderXML 架构。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
Write(Stream, XmlNamespaceManager)

使用XmlNamespaceManager指定的 XML 架构写入提供的 Stream XML 架构。

Write(Stream)

将 XML 架构写入提供的数据流。

Write(TextWriter, XmlNamespaceManager)

将 XML 架构写入提供的 TextWriter

Write(TextWriter)

将 XML 架构写入提供的 TextWriter

Write(XmlWriter, XmlNamespaceManager)

将 XML 架构写入提供的 XmlWriter

Write(XmlWriter)

将 XML 架构写入提供的 XmlWriter

适用于

另请参阅