XmlSchema 類別

定義

XML 結構描述的記憶體內部表示,其依據為全球資訊網協會 (W3C) XML Schema Part 1:Structures (XML 結構描述第 1 部分:結構) 及 XML Schema Part 2:Datatypes (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. 例如,XmlSchemaExceptionSourceUri 屬性會傳回造成例外狀況之架構檔案的 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 類別的新執行個體。Initializes a new instance of the XmlSchema class.

欄位

InstanceNamespace

XML 結構描述執行個體命名空間。The XML schema instance namespace. 這個欄位是常數。This field is constant.

Namespace

XML 結構描述命名空間。The XML schema namespace. 這個欄位是常數。This field is constant.

屬性

AttributeFormDefault

取得或設定在結構描述目標命名空間中宣告的屬性 (Attribute) 格式。Gets or sets the form for attributes declared in the target namespace of the schema.

AttributeGroups

取得結構描述中所有全域屬性群組的後結構描述編譯值。Gets the post-schema-compilation value of all the global attribute groups in the schema.

Attributes

取得結構描述中所有屬性的後結構描述編譯值。Gets the post-schema-compilation value for all the attributes in the schema.

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

取得或設定在結構描述目標命名空間中宣告的元素格式。Gets or sets the form for elements declared in the target namespace of the schema.

Elements

取得結構描述中所有元素的後結構描述編譯值。Gets the post-schema-compilation value for all the elements in the schema.

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

取得結構描述中所有群組的後結構描述編譯值。Gets the post-schema-compilation value of all the groups in the schema.

Id

取得或設定字串 ID。Gets or sets the string ID.

Includes

取得包含和匯入結構描述的集合。Gets the collection of included and imported schemas.

IsCompiled

指出結構描述是否已編譯。Indicates if the schema has been compiled.

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

取得或設定 schema 項目參考之檔案中的行號。Gets or sets the line number in the file to which the schema element refers.

(繼承來源 XmlSchemaObject)
LinePosition

取得或設定 schema 項目參考之檔案中的行位置。Gets or sets the line position in the file to which the schema element refers.

(繼承來源 XmlSchemaObject)
Namespaces

取得或設定 XmlSerializerNamespaces,以便與這個結構描述物件一起使用。Gets or sets the XmlSerializerNamespaces to use with this schema object.

(繼承來源 XmlSchemaObject)
Notations

取得結構描述中所有標記法的後結構描述編譯值。Gets the post-schema-compilation value for all notations in the schema.

Parent

取得或設定這個 XmlSchemaObject 的父項。Gets or sets the parent of this XmlSchemaObject.

(繼承來源 XmlSchemaObject)
SchemaTypes

取得結構描述中所有結構描述類型的後結構描述編譯值。Gets the post-schema-compilation value of all schema types in the schema.

SourceUri

取得或設定載入結構描述之檔案的來源位置。Gets or sets the source location for the file that loaded the schema.

(繼承來源 XmlSchemaObject)
TargetNamespace

取得或設定結構描述目標命名空間的統一資源識別元 (URI)。Gets or sets the Uniform Resource Identifier (URI) of the schema target namespace.

UnhandledAttributes

取得或設定不屬於結構描述目標命名空間的限定屬性。Gets or sets the qualified attributes which do not belong to the schema target namespace.

Version

取得或設定結構描述的版本。Gets or sets the version of the schema.

方法

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)

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

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Read(Stream, ValidationEventHandler)

從提供的資料流讀取 XML 結構描述。Reads an XML Schema from the supplied stream.

Read(TextReader, ValidationEventHandler)

從提供的 TextReader 讀取 XML 結構描述。Reads an XML Schema from the supplied TextReader.

Read(XmlReader, ValidationEventHandler)

從提供的 XmlReader 讀取 XML 結構描述。Reads an XML Schema from the supplied XmlReader.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Write(Stream)

將 XML 結構描述寫入提供的資料流。Writes the XML Schema to the supplied data stream.

Write(Stream, XmlNamespaceManager)

使用指定的 Stream,將 XML 結構描述寫入提供的 XmlNamespaceManagerWrites the XML Schema to the supplied Stream using the XmlNamespaceManager specified.

Write(TextWriter)

將 XML 結構描述寫入提供的 TextWriterWrites the XML Schema to the supplied TextWriter.

Write(TextWriter, XmlNamespaceManager)

將 XML 結構描述寫入提供的 TextWriterWrites the XML Schema to the supplied TextWriter.

Write(XmlWriter)

將 XML 結構描述寫入提供的 XmlWriterWrites the XML Schema to the supplied XmlWriter.

Write(XmlWriter, XmlNamespaceManager)

將 XML 結構描述寫入提供的 XmlWriterWrites the XML Schema to the supplied XmlWriter.

適用於

另請參閱