XmlSchema Klasa

Definicja

Reprezentacja w pamięci schematu XML, jak określono w schemacie XML World Wide Web Consortium (W3C) Schema Part 1: Structures and XML Schema Part 2: Datatypes (Schemat 1: Struktury i schemat XML— część 2: Typy danych).

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
Dziedziczenie
XmlSchema
Dziedziczenie

Przykłady

Poniższy przykład tworzy definicję schematu.

#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

Poniższy plik XML jest generowany dla poprzedniego przykładu kodu.


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

Uwagi

Ważne

  • Nie używaj schematów z nieznanych lub niezaufanych źródeł ani lokalizacji. Spowoduje to naruszenie zabezpieczeń kodu.
  • Schematy XML (w tym schematy wbudowane) są z natury narażone na ataki typu "odmowa usługi"; nie akceptuj ich w niezaufanych scenariuszach.
  • Wyjątki zgłaszane w wyniku użycia XmlSchema klasy, takiej jak XmlSchemaException klasa, mogą zawierać poufne informacje, które nie powinny być widoczne w niezaufanych scenariuszach. Na przykład SourceUri właściwość identyfikatora XmlSchemaException URI zwraca ścieżkę identyfikatora URI do pliku schematu, który spowodował wyjątek. Właściwość SourceUri nie powinna być uwidoczniona w niezaufanych scenariuszach. Wyjątki powinny być prawidłowo obsługiwane, więc te poufne informacje nie są widoczne w niezaufanych scenariuszach.

Konstruktory

XmlSchema()

Inicjuje nowe wystąpienie klasy XmlSchema.

Pola

InstanceNamespace

Przestrzeń nazw wystąpienia schematu XML. To pole jest stałe.

Namespace

Przestrzeń nazw schematu XML. To pole jest stałe.

Właściwości

AttributeFormDefault

Pobiera lub ustawia formularz atrybutów zadeklarowanych w docelowej przestrzeni nazw schematu.

AttributeGroups

Pobiera wartość po kompilacji schematu wszystkich globalnych grup atrybutów w schemacie.

Attributes

Pobiera wartość kompilacji po schemacie dla wszystkich atrybutów w schemacie.

BlockDefault

Pobiera lub ustawia atrybut, który ustawia blockDefault domyślną wartość atrybutu block dla elementu i typów złożonych w targetNamespace schemacie.

ElementFormDefault

Pobiera lub ustawia formularz dla elementów zadeklarowanych w docelowej przestrzeni nazw schematu.

Elements

Pobiera wartość kompilacji po schemacie dla wszystkich elementów w schemacie.

FinalDefault

Pobiera lub ustawia atrybut, który ustawia finalDefault domyślną wartość atrybutu final na elementach i typach złożonych w docelowej przestrzeni nazw schematu.

Groups

Pobiera wartość kompilacji po schemacie wszystkich grup w schemacie.

Id

Pobiera lub ustawia identyfikator ciągu.

Includes

Pobiera kolekcję dołączonych i zaimportowanych schematów.

IsCompiled

Wskazuje, czy schemat został skompilowany.

Items

Pobiera kolekcję elementów schematu w schemacie i służy do dodawania nowych typów elementów na schema poziomie elementu.

LineNumber

Pobiera lub ustawia numer wiersza w pliku, do którego schema odwołuje się element.

(Odziedziczone po XmlSchemaObject)
LinePosition

Pobiera lub ustawia położenie wiersza w pliku, do którego schema odwołuje się element.

(Odziedziczone po XmlSchemaObject)
Namespaces

Pobiera lub ustawia XmlSerializerNamespaces obiekt do użycia z tym obiektem schematu.

(Odziedziczone po XmlSchemaObject)
Notations

Pobiera wartość po kompilacji schematu dla wszystkich notacji w schemacie.

Parent

Pobiera lub ustawia element nadrzędny tego XmlSchemaObjectobiektu .

(Odziedziczone po XmlSchemaObject)
SchemaTypes

Pobiera wartość po kompilacji schematu wszystkich typów schematów w schemacie.

SourceUri

Pobiera lub ustawia lokalizację źródłową pliku, który załadował schemat.

(Odziedziczone po XmlSchemaObject)
TargetNamespace

Pobiera lub ustawia identyfikator URI (Uniform Resource Identifier) przestrzeni nazw docelowej schematu.

UnhandledAttributes

Pobiera lub ustawia kwalifikowane atrybuty, które nie należą do docelowej przestrzeni nazw schematu.

Version

Pobiera lub ustawia wersję schematu.

Metody

Compile(ValidationEventHandler)
Nieaktualne.
Nieaktualne.
Nieaktualne.

Kompiluje model obiektów schematu XML (SOM) do informacji o schemacie na potrzeby walidacji. Służy do sprawdzania składniowej i semantycznej struktury skompilowanych programowo som. Sprawdzanie poprawności semantycznej jest wykonywane podczas kompilacji.

Compile(ValidationEventHandler, XmlResolver)
Nieaktualne.
Nieaktualne.
Nieaktualne.

Kompiluje model obiektów schematu XML (SOM) do informacji o schemacie na potrzeby walidacji. Służy do sprawdzania składniowej i semantycznej struktury skompilowanych programowo som. Sprawdzanie poprawności semantycznej jest wykonywane podczas kompilacji.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Read(Stream, ValidationEventHandler)

Odczytuje schemat XML z dostarczonego strumienia.

Read(TextReader, ValidationEventHandler)

Odczytuje schemat XML z dostarczonego TextReaderelementu .

Read(XmlReader, ValidationEventHandler)

Odczytuje schemat XML z dostarczonego XmlReaderelementu .

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
Write(Stream)

Zapisuje schemat XML do dostarczonego strumienia danych.

Write(Stream, XmlNamespaceManager)

Zapisuje schemat XML do dostarczonego Stream przy użyciu określonego XmlNamespaceManager .

Write(TextWriter)

Zapisuje schemat XML w podanym TextWriterpliku .

Write(TextWriter, XmlNamespaceManager)

Zapisuje schemat XML w podanym TextWriterpliku .

Write(XmlWriter)

Zapisuje schemat XML w podanym XmlWriterpliku .

Write(XmlWriter, XmlNamespaceManager)

Zapisuje schemat XML w podanym XmlWriterpliku .

Dotyczy

Zobacz też