共用方式為


XmlSchema 類別

定義

XML 架構的記憶體表示法,依據萬維網聯盟(W3C) XML 架構第一部分:結構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 不應在不受信任的情境中公開。 例外情況應妥善處理,避免敏感資訊在不可信的情況下暴露。

建構函式

名稱 Description
XmlSchema()

初始化 XmlSchema 類別的新執行個體。

欄位

名稱 Description
InstanceNamespace

XML 結構實例命名空間。 此欄位是常數。

Namespace

XML 結構命名空間。 此欄位是常數。

屬性

名稱 Description
AttributeFormDefault

取得或設定在結構目標命名空間中宣告的屬性表單。

AttributeGroups

取得結構中所有全域屬性群組的後 schema 編譯值。

Attributes

取得結構中所有屬性的後 schema-compilation 值。

BlockDefault

取得或設定屬性blockDefault,該屬性設定該屬性在結構元素targetNamespace與複雜型態上的預設值block

ElementFormDefault

取得或設定在結構目標命名空間中宣告的元素的形式。

Elements

取得結構中所有元素的後 schema-compile 值。

FinalDefault

取得或設定 finalDefault 屬性,該屬性設定該屬性在結構目標命名空間中元素與複雜型態的預設值 final

Groups

取得結構中所有群組的後 schema 編譯值。

Id

取得或設定字串 ID。

Includes

取得包含與匯入的結構集合。

IsCompiled

顯示結構是否已被編譯完成。

Items

取得結構中結構元素的集合,並用於在元素層級新增元素類型 schema

LineNumber

取得或設定該 schema 元素所指檔案中的行號。

(繼承來源 XmlSchemaObject)
LinePosition

取得或設定該元素所指檔案 schema 中的行位。

(繼承來源 XmlSchemaObject)
Namespaces

用這個結構物件取得或設定 XmlSerializerNamespaces 使用 。

(繼承來源 XmlSchemaObject)
Notations

取得結構中所有符號的後 schema-compilation 值。

Parent

取得或設定此 XmlSchemaObject的父節點。

(繼承來源 XmlSchemaObject)
SchemaTypes

取得結構編譯後所有結構類型後的值。

SourceUri

取得或設定載入結構檔案的來源位置。

(繼承來源 XmlSchemaObject)
TargetNamespace

取得或設定結構目標命名空間的統一資源識別碼(URI)。

UnhandledAttributes

取得或設定不屬於結構目標命名空間的合格屬性。

Version

取得或設定結構的版本。

方法

名稱 Description
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)

使用指定方式將 XML 架構寫入所提供StreamXmlNamespaceManager的內容。

Write(Stream)

將 XML 架構寫入所提供的資料串流。

Write(TextWriter, XmlNamespaceManager)

將 XML 架構寫入所提供的 TextWriter

Write(TextWriter)

將 XML 架構寫入所提供的 TextWriter

Write(XmlWriter, XmlNamespaceManager)

將 XML 架構寫入所提供的 XmlWriter

Write(XmlWriter)

將 XML 架構寫入所提供的 XmlWriter

適用於

另請參閱