共用方式為


XmlSchemaSimpleType 類別

定義

將來自 XML 結構描述之簡單內容的 simpleType 元素,依全球資訊網協會 (W3C) 的規定表示。 這個類別 (Class) 定義簡單類型。 簡單類型可以指定具有純文字內容之屬性 (Attribute) 或元素值的資訊和條件約束。

public ref class XmlSchemaSimpleType : System::Xml::Schema::XmlSchemaType
public class XmlSchemaSimpleType : System.Xml.Schema.XmlSchemaType
type XmlSchemaSimpleType = class
    inherit XmlSchemaType
Public Class XmlSchemaSimpleType
Inherits XmlSchemaType
繼承

範例

下列範例顯示 類別的使用 XmlSchemaSimpleType

#using <System.Xml.dll>

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

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

public:
    static void Main()
    {
        XmlSchema^ schema = gcnew XmlSchema();

        // <xs:simpleType name="LotteryNumber">
        XmlSchemaSimpleType^ LotteryNumberType = gcnew XmlSchemaSimpleType();
        LotteryNumberType->Name = "LotteryNumber";

        // <xs:restriction base="xs:int">
        XmlSchemaSimpleTypeRestriction^ LotteryNumberRestriction = gcnew XmlSchemaSimpleTypeRestriction();
        LotteryNumberRestriction->BaseTypeName = gcnew XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");

        // <xs:minInclusive value="1"/>
        XmlSchemaMinInclusiveFacet^ minInclusive = gcnew XmlSchemaMinInclusiveFacet();
        minInclusive->Value = "1";
        LotteryNumberRestriction->Facets->Add(minInclusive);

        // <xs:maxInclusive value="99"/>
        XmlSchemaMaxInclusiveFacet^ maxInclusive = gcnew XmlSchemaMaxInclusiveFacet();
        maxInclusive->Value = "99";
        LotteryNumberRestriction->Facets->Add(maxInclusive);

        LotteryNumberType->Content = LotteryNumberRestriction;
        schema->Items->Add(LotteryNumberType);

        // <xs:simpleType name="LotteryNumberList">
        XmlSchemaSimpleType^ LotteryNumberListType = gcnew XmlSchemaSimpleType();
        LotteryNumberListType->Name = "LotteryNumberList";

        // <xs:list itemType="LotteryNumber"/>
        XmlSchemaSimpleTypeList^ list = gcnew XmlSchemaSimpleTypeList();
        list->ItemTypeName = gcnew XmlQualifiedName("LotteryNumber", "");
        LotteryNumberListType->Content = list;

        schema->Items->Add(LotteryNumberListType);

        // <xs:simpleType name="LotteryNumbers">
        XmlSchemaSimpleType^ LotteryNumbersType = gcnew XmlSchemaSimpleType();
        LotteryNumbersType->Name = "LotteryNumbers";

        // <xs:restriction base="LotteryNumberList">
        XmlSchemaSimpleTypeRestriction^ LotteryNumbersRestriction = gcnew XmlSchemaSimpleTypeRestriction();
        LotteryNumbersRestriction->BaseTypeName = gcnew XmlQualifiedName("LotteryNumberList", "");

        // <xs:length value="5"/>
        XmlSchemaLengthFacet^ length = gcnew XmlSchemaLengthFacet();
        length->Value = "5";
        LotteryNumbersRestriction->Facets->Add(length);

        LotteryNumbersType->Content = LotteryNumbersRestriction;

        schema->Items->Add(LotteryNumbersType);

        // <xs:element name="TodaysLottery" type="LotteryNumbers">
        XmlSchemaElement^ TodaysLottery = gcnew XmlSchemaElement();
        TodaysLottery->Name = "TodaysLottery";
        TodaysLottery->SchemaTypeName = gcnew XmlQualifiedName("LotteryNumbers", "");

        schema->Items->Add(TodaysLottery);

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

        XmlSchema^ compiledSchema = nullptr;

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

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:simpleType name="LotteryNumber">
        XmlSchemaSimpleType LotteryNumberType = new XmlSchemaSimpleType();
        LotteryNumberType.Name = "LotteryNumber";

        // <xs:restriction base="xs:int">
        XmlSchemaSimpleTypeRestriction LotteryNumberRestriction = new XmlSchemaSimpleTypeRestriction();
        LotteryNumberRestriction.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");

        // <xs:minInclusive value="1"/>
        XmlSchemaMinInclusiveFacet minInclusive = new XmlSchemaMinInclusiveFacet();
        minInclusive.Value = "1";
        LotteryNumberRestriction.Facets.Add(minInclusive);

        // <xs:maxInclusive value="99"/>
        XmlSchemaMaxInclusiveFacet maxInclusive = new XmlSchemaMaxInclusiveFacet();
        maxInclusive.Value = "99";
        LotteryNumberRestriction.Facets.Add(maxInclusive);

        LotteryNumberType.Content = LotteryNumberRestriction;
        schema.Items.Add(LotteryNumberType);

        // <xs:simpleType name="LotteryNumberList">
        XmlSchemaSimpleType LotteryNumberListType = new XmlSchemaSimpleType();
        LotteryNumberListType.Name = "LotteryNumberList";

        // <xs:list itemType="LotteryNumber"/>
        XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
        list.ItemTypeName = new XmlQualifiedName("LotteryNumber", "");
        LotteryNumberListType.Content = list;

        schema.Items.Add(LotteryNumberListType);

        // <xs:simpleType name="LotteryNumbers">
        XmlSchemaSimpleType LotteryNumbersType = new XmlSchemaSimpleType();
        LotteryNumbersType.Name = "LotteryNumbers";

        // <xs:restriction base="LotteryNumberList">
        XmlSchemaSimpleTypeRestriction LotteryNumbersRestriction = new XmlSchemaSimpleTypeRestriction();
        LotteryNumbersRestriction.BaseTypeName = new XmlQualifiedName("LotteryNumberList", "");

        // <xs:length value="5"/>
        XmlSchemaLengthFacet length = new XmlSchemaLengthFacet();
        length.Value = "5";
        LotteryNumbersRestriction.Facets.Add(length);

        LotteryNumbersType.Content = LotteryNumbersRestriction;

        schema.Items.Add(LotteryNumbersType);

        // <xs:element name="TodaysLottery" type="LotteryNumbers">
        XmlSchemaElement TodaysLottery = new XmlSchemaElement();
        TodaysLottery.Name = "TodaysLottery";
        TodaysLottery.SchemaTypeName = new XmlQualifiedName("LotteryNumbers", "");

        schema.Items.Add(TodaysLottery);

        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 Strict On
Option Explicit On

Imports System.Xml
Imports System.Xml.Schema

Class XMLSchemaExamples

    Public Shared Sub Main()

        Dim schema As New XmlSchema()

        ' <xs:simpleType name="LotteryNumber">
        Dim LotteryNumberType As New XmlSchemaSimpleType()
        LotteryNumberType.Name = "LotteryNumber"

        ' <xs:restriction base="xs:int">
        Dim LotteryNumberRestriction As New XmlSchemaSimpleTypeRestriction()
        LotteryNumberRestriction.BaseTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")

        ' <xs:minInclusive value="1"/>
        Dim minInclusive As New XmlSchemaMinInclusiveFacet()
        minInclusive.Value = "1"
        LotteryNumberRestriction.Facets.Add(minInclusive)

        ' <xs:maxInclusive value="99"/>
        Dim maxInclusive As New XmlSchemaMaxInclusiveFacet()
        maxInclusive.Value = "99"
        LotteryNumberRestriction.Facets.Add(maxInclusive)

        LotteryNumberType.Content = LotteryNumberRestriction
        schema.Items.Add(LotteryNumberType)

        ' <xs:simpleType name="LotteryNumberList">
        Dim LotteryNumberListType As New XmlSchemaSimpleType()
        LotteryNumberListType.Name = "LotteryNumberList"

        ' <xs:list itemType="LotteryNumber"/>
        Dim list As New XmlSchemaSimpleTypeList()
        list.ItemTypeName = New XmlQualifiedName("LotteryNumber", "")
        LotteryNumberListType.Content = list

        schema.Items.Add(LotteryNumberListType)

        ' <xs:simpleType name="LotteryNumbers">
        Dim LotteryNumbersType As New XmlSchemaSimpleType()
        LotteryNumbersType.Name = "LotteryNumbers"

        ' <xs:restriction base="LotteryNumberList">
        Dim LotteryNumbersRestriction As New XmlSchemaSimpleTypeRestriction()
        LotteryNumbersRestriction.BaseTypeName = New XmlQualifiedName("LotteryNumberList", "")

        ' <xs:length value="5"/>
        Dim length As New XmlSchemaLengthFacet()
        length.Value = "5"
        LotteryNumbersRestriction.Facets.Add(length)

        LotteryNumbersType.Content = LotteryNumbersRestriction

        schema.Items.Add(LotteryNumbersType)

        ' <xs:element name="TodaysLottery" type="LotteryNumbers">
        Dim TodaysLottery As New XmlSchemaElement()
        TodaysLottery.Name = "TodaysLottery"
        TodaysLottery.SchemaTypeName = New XmlQualifiedName("LotteryNumbers", "")

        schema.Items.Add(TodaysLottery)

        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:simpleType name="LotteryNumber">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="99"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="LotteryNumberList">
        <xs:list itemType="LotteryNumber"/>
    </xs:simpleType>
    
    <xs:simpleType name="LotteryNumbers">
        <xs:restriction base="LotteryNumberList">
            <xs:length value="5"/>
        </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="TodaysLottery" type="LotteryNumbers">
    </xs:element>
    
</xs:schema>

備註

簡單型別是從現有簡單型別 (內建資料型別和衍生簡單型別) 衍生而定義的。 簡單型別不能包含項目,也不能有屬性。

建構函式

XmlSchemaSimpleType()

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

屬性

Annotation

取得或設定 annotation 屬性。

(繼承來源 XmlSchemaAnnotated)
BaseSchemaType
已過時。
已過時。
已過時。

取得編譯後物件型別或內建 XML 結構描述定義語言 (XSD) 資料型別、simpleType 項目或 complexType 項目。 這是後結構描述編譯資訊集屬性。

(繼承來源 XmlSchemaType)
BaseXmlSchemaType

取得此結構描述型別之基底型別的編譯後值。

(繼承來源 XmlSchemaType)
Content

取得或設定 XmlSchemaSimpleTypeUnionXmlSchemaSimpleTypeListXmlSchemaSimpleTypeRestriction 之一。

Datatype

取得複雜型別之資料型別的編譯後值。

(繼承來源 XmlSchemaType)
DerivedBy

取得有關此項目如何從其基底型別衍生的編譯後資訊。

(繼承來源 XmlSchemaType)
Final

取得或設定型別衍生的 final 屬性,指出是否允許進一步衍生。

(繼承來源 XmlSchemaType)
FinalResolved

取得 Final 屬性的編譯後值。

(繼承來源 XmlSchemaType)
Id

取得或設定字串 ID。

(繼承來源 XmlSchemaAnnotated)
IsMixed

取得或設定值,指出這個型別是否具有混合內容模型。 這個屬性只在複雜型別中有效。

(繼承來源 XmlSchemaType)
LineNumber

取得或設定 schema 項目參考之檔案中的行號。

(繼承來源 XmlSchemaObject)
LinePosition

取得或設定 schema 項目參考之檔案中的行位置。

(繼承來源 XmlSchemaObject)
Name

取得或設定類型的名稱。

(繼承來源 XmlSchemaType)
Namespaces

取得或設定 XmlSerializerNamespaces,以便與這個結構描述物件一起使用。

(繼承來源 XmlSchemaObject)
Parent

取得或設定這個 XmlSchemaObject 的父項。

(繼承來源 XmlSchemaObject)
QualifiedName

取得從這個型別的 Name 屬性 (Attribute) 建置之型別的限定名稱 (Qualified Name)。 這是後結構描述編譯屬性。

(繼承來源 XmlSchemaType)
SourceUri

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

(繼承來源 XmlSchemaObject)
TypeCode

取得型別的 XmlTypeCode

(繼承來源 XmlSchemaType)
UnhandledAttributes

取得或設定不屬於目前結構描述之目標命名空間的限定屬性 (Attribute)。

(繼承來源 XmlSchemaAnnotated)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於