Compartilhar via


XmlSchemaSimpleType Classe

Definição

Representa o elemento simpleType para conteúdo simples do Esquema XML, conforme especificado pelo W3C (World Wide Web Consortium). Esta classe define um tipo simples. Tipos simples podem especificar informações e restrições para o valor dos atributos ou elementos com conteúdo somente texto.

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
Herança

Exemplos

O exemplo a seguir mostra o uso da XmlSchemaSimpleType classe.

#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

O arquivo XML a seguir é gerado para o exemplo de código anterior.

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

Comentários

Os tipos simples são definidos por derivação dos tipos simples existentes (tipos de dados internos e tipos simples derivados). Um tipo simples não pode conter elementos e não pode ter atributos.

Construtores

XmlSchemaSimpleType()

Inicializa uma nova instância da classe XmlSchemaSimpleType.

Propriedades

Annotation

Obtém ou define a propriedade annotation.

(Herdado de XmlSchemaAnnotated)
BaseSchemaType
Obsoleto.
Obsoleto.
Obsoleto.

Obtém o tipo de objeto pós-compilação ou o tipo de dados XSD (Linguagem de Definição de Esquema XML) interno, elemento simpleType ou elemento complexType. Esta é uma propriedade infoset pós-compilação do esquema.

(Herdado de XmlSchemaType)
BaseXmlSchemaType

Obtém o valor pós-compilação para o tipo base desse tipo de esquema.

(Herdado de XmlSchemaType)
Content

Obtém ou define XmlSchemaSimpleTypeUnion, XmlSchemaSimpleTypeList ou XmlSchemaSimpleTypeRestriction.

Datatype

Obtém o valor de pós-compilação para o tipo de dados do tipo complexo.

(Herdado de XmlSchemaType)
DerivedBy

Obtém as informações pós-compilação sobre como esse elemento foi derivado de seu tipo base.

(Herdado de XmlSchemaType)
Final

Obtém ou define o atributo final da derivação de tipo que indica se mais derivações são permitidas.

(Herdado de XmlSchemaType)
FinalResolved

Obtém o valor de pós-compilação da propriedade Final.

(Herdado de XmlSchemaType)
Id

Obtém ou define a ID de cadeia de caracteres.

(Herdado de XmlSchemaAnnotated)
IsMixed

Obtém ou define um valor que indica se esse tipo tem um modelo de conteúdo misto. Essa propriedade só é válida em um tipo complexo.

(Herdado de XmlSchemaType)
LineNumber

Obtém ou define o número de linha no arquivo ao qual o elemento schema se refere.

(Herdado de XmlSchemaObject)
LinePosition

Obtém ou define a posição da linha no arquivo ao qual o elemento schema se refere.

(Herdado de XmlSchemaObject)
Name

Obtém ou define o nome do tipo.

(Herdado de XmlSchemaType)
Namespaces

Obtém ou define o XmlSerializerNamespaces a ser usado com esse objeto de esquema.

(Herdado de XmlSchemaObject)
Parent

Obtém ou define o pai deste XmlSchemaObject.

(Herdado de XmlSchemaObject)
QualifiedName

Obtém o nome qualificado para o tipo construído com base no atributo Name desse tipo. Esta é uma propriedade pós-compilação do esquema.

(Herdado de XmlSchemaType)
SourceUri

Obtém ou define o local de origem para o arquivo que carregou o esquema.

(Herdado de XmlSchemaObject)
TypeCode

Obtém o XmlTypeCode do tipo.

(Herdado de XmlSchemaType)
UnhandledAttributes

Obtém ou define os atributos qualificados que não pertencem ao namespace de destino do esquema atual.

(Herdado de XmlSchemaAnnotated)

Métodos

Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Object atual.

(Herdado de Object)
ToString()

Retorna uma cadeia de caracteres que representa o objeto atual.

(Herdado de Object)

Aplica-se a