XmlSchema Třída

Definice

Reprezentace schématu XML v paměti, jak je uvedeno ve schématu XML World Wide Web Consortium (W3C) část 1: Struktury a schéma XML část 2: Datové typy].

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
Dědičnost
XmlSchema
Dědičnost

Příklady

Následující příklad vytvoří definici schématu.

#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

Následující soubor XML se vygeneruje pro předchozí příklad kódu.


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

Poznámky

Důležité

  • Nepoužívejte schémata z neznámých nebo nedůvěryhodných zdrojů nebo umístění. Tím dojde k ohrožení zabezpečení vašeho kódu.
  • Schémata XML (včetně vložených schémat) jsou ze své podstaty zranitelná vůči útokům na odepření služeb; nepřijme je v nedůvěryhodných scénářích.
  • Výjimky vyvolané v důsledku použití XmlSchema třídy, například XmlSchemaException třídy, mohou obsahovat citlivé informace, které by neměly být vystaveny v nedůvěryhodných scénářích. SourceUri Například vlastnost XmlSchemaException vrátí cestu URI k souboru schématu, který způsobil výjimku. Vlastnost SourceUri by neměla být zpřístupněna v nedůvěryhodných scénářích. Výjimky by se měly správně zpracovat, takže tyto citlivé informace se v nedůvěryhodných scénářích nezpřístupní.

Konstruktory

XmlSchema()

Inicializuje novou instanci XmlSchema třídy.

Pole

InstanceNamespace

Obor názvů instance schématu XML. Toto pole je konstantní.

Namespace

Obor názvů schématu XML. Toto pole je konstantní.

Vlastnosti

AttributeFormDefault

Získá nebo nastaví formulář pro atributy deklarované v cílovém oboru názvů schématu.

AttributeGroups

Získá hodnotu post-schema-kompilace všech globálních skupin atributů ve schématu.

Attributes

Získá hodnotu post-schema-kompilace pro všechny atributy ve schématu.

BlockDefault

Získá nebo nastaví blockDefault atribut, který nastaví výchozí hodnotu atributu block pro element a komplexní typy ve targetNamespace schématu.

ElementFormDefault

Získá nebo nastaví formulář pro elementy deklarované v cílovém oboru názvů schématu.

Elements

Získá hodnotu post-schema-kompilace pro všechny prvky ve schématu.

FinalDefault

Získá nebo nastaví finalDefault atribut, který nastaví výchozí hodnotu atributu final na elementy a komplexní typy v cílovém oboru názvů schématu.

Groups

Získá hodnotu post-schema-kompilace všech skupin ve schématu.

Id

Získá nebo nastaví ID řetězce.

Includes

Získá kolekci zahrnutých a importovaných schémat.

IsCompiled

Označuje, jestli bylo schéma zkompilováno.

Items

Získá kolekci prvků schématu ve schématu a slouží k přidání nových typů elementů na úrovni elementu schema .

LineNumber

Získá nebo nastaví číslo řádku v souboru, na který schema prvek odkazuje.

(Zděděno od XmlSchemaObject)
LinePosition

Získá nebo nastaví pozici čáry v souboru, na který schema prvek odkazuje.

(Zděděno od XmlSchemaObject)
Namespaces

Získá nebo nastaví XmlSerializerNamespaces použití s tímto objektem schématu.

(Zděděno od XmlSchemaObject)
Notations

Získá hodnotu post-schema-kompilace pro všechny notace ve schématu.

Parent

Získá nebo nastaví nadřazený objekt tohoto XmlSchemaObject.

(Zděděno od XmlSchemaObject)
SchemaTypes

Získá hodnotu post-schema-kompilace všech typů schématu ve schématu.

SourceUri

Získá nebo nastaví zdrojové umístění pro soubor, který načetl schéma.

(Zděděno od XmlSchemaObject)
TargetNamespace

Získá nebo nastaví identifikátor URI (Uniform Resource Identifier) cílového oboru názvů schématu.

UnhandledAttributes

Získá nebo nastaví kvalifikované atributy, které nepatří do cílového oboru názvů schématu.

Version

Získá nebo nastaví verzi schématu.

Metody

Compile(ValidationEventHandler)
Zastaralé.
Zastaralé.
Zastaralé.

Zkompiluje objektový model schématu XML (SOM) do informací o schématu pro ověření. Slouží ke kontrole syntaktické a sémantické struktury programově sestaveného SOM. Při kompilaci se provádí sémantická kontrola ověření.

Compile(ValidationEventHandler, XmlResolver)
Zastaralé.
Zastaralé.
Zastaralé.

Zkompiluje objektový model schématu XML (SOM) do informací o schématu pro ověření. Slouží ke kontrole syntaktické a sémantické struktury programově sestaveného SOM. Při kompilaci se provádí sémantická kontrola ověření.

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Type Získá aktuální instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
Read(Stream, ValidationEventHandler)

Načte schéma XML ze zadaného datového proudu.

Read(TextReader, ValidationEventHandler)

Čte schéma XML ze zadaného TextReaderschématu .

Read(XmlReader, ValidationEventHandler)

Čte schéma XML ze zadaného XmlReaderschématu .

ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)
Write(Stream)

Zapíše schéma XML do zadaného datového proudu.

Write(Stream, XmlNamespaceManager)

Zapíše schéma XML do zadaného Stream schématu XmlNamespaceManager .

Write(TextWriter)

Zapíše schéma XML do zadané TextWriter.

Write(TextWriter, XmlNamespaceManager)

Zapíše schéma XML do zadané TextWriter.

Write(XmlWriter)

Zapíše schéma XML do zadané XmlWriter.

Write(XmlWriter, XmlNamespaceManager)

Zapíše schéma XML do zadané XmlWriter.

Platí pro

Viz také