Erstellen von XML-Schemata

Aktualisiert: November 2007

Die Klassen im System.Xml.Schema-Namespace werden den Strukturen zugeordnet, die in der XML-Schemaempfehlung des W3C (World Wide Web Consortium) definiert sind, und können zum speicherinternen Erstellen von XML-Schemata verwendet werden.

Erstellen eines XML-Schemas

Im folgenden Codebeispiel wird mithilfe der SOM-API speicherintern ein Kunden-XML-Schema erstellt.

Erstellen von Elementen und Attributen

In den Codebeispielen werden die Kundenschemata von unten nach oben erstellt, wobei zuerst die untergeordneten Elemente und Attribute und die entsprechenden Typen erstellt werden, und dann die Elemente der obersten Ebene.

Im folgenden Codebeispiel werden das FirstName-Element und das LastName-Element sowie das CustomerId-Attribut des Kundenschemas mithilfe der XmlSchemaElement-Klasse und der XmlSchemaAttribute-Klasse des SOMs erstellt. Außer der Name-Eigenschaften der XmlSchemaElement-Klasse und der XmlSchemaAttribute-Klasse, die dem "name"-Attribut des <xs:element />-Elements und des <xs:attribute />-Elements in einem XML-Schema entsprechen, verfügen alle anderen im Schema zulässigen Attribute (defaultValue, fixedValue, form usw.) über entsprechende Eigenschaften in der XmlSchemaElement-Klasse und der XmlSchemaAttribute-Klasse.

' Create the FirstName and LastName elements.
Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
firstNameElement.Name = "FirstName"
Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
lastNameElement.Name = "LastName"

' Create CustomerId attribute.
Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
idAttribute.Name = "CustomerId"
idAttribute.Use = XmlSchemaUse.Required
// Create the FirstName and LastName elements.
XmlSchemaElement firstNameElement = new XmlSchemaElement();
firstNameElement.Name = "FirstName";
XmlSchemaElement lastNameElement = new XmlSchemaElement();
lastNameElement.Name = "LastName";

// Create CustomerId attribute.
XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
idAttribute.Name = "CustomerId";
idAttribute.Use = XmlSchemaUse.Required;
        // Create the FirstName and LastName elements.
        XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
        firstNameElement->Name = "FirstName";
        XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
        lastNameElement->Name = "LastName";

        // Create CustomerId attribute.
        XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
        idAttribute->Name = "CustomerId";
        idAttribute->Use = XmlSchemaUse::Required;

Erstellen von Schematypen

Der Inhalt von Elementen und Attributen wird von den jeweiligen Typen definiert. Zum Erstellen von Elementen und Attributen, deren Typen einem der integrierten Schematypen entsprechen, wird die SchemaTypeName-Eigenschaft der XmlSchemaElement-Klasse oder der XmlSchemaAttribute-Klasse mithilfe der XmlQualifiedName-Klasse mit dem entsprechenden qualifizierten Namen des integrierten Typs festgelegt. Zum Erstellen eines benutzerdefinierten Typs für Elemente und Attribute wird mithilfe der XmlSchemaSimpleType-Klasse oder der XmlSchemaComplexType-Klasse ein neuer einfacher oder komplexer Typ erstellt.

Hinweis:

Zum Erstellen unbenannter einfacher oder komplexer Typen, die anonyme untergeordnete Elemente eines Elements oder Attributs sind (für Attribute gelten nur einfache Typen), legen Sie die SchemaType-Eigenschaft der XmlSchemaElement-Klasse oder der XmlSchemaAttribute-Klasse anstelle der SchemaTypeName-Eigenschaft der XmlSchemaElement-Klasse oder der XmlSchemaAttribute-Klasse auf den unbenannten einfachen oder komplexen Typ fest.

Bei XML-Schemata können durch Einschränkung anonyme und benannte einfache Typen von anderen einfachen Typen (integriert oder benutzerdefiniert) abgeleitet werden. Außerdem können diese als Liste oder Union von anderen einfachen Typen erstellt werden. Mithilfe der XmlSchemaSimpleTypeRestriction-Klasse wird ein einfacher Typ durch Einschränkung des integrierten xs:string-Typs erstellt. Sie können auch mit der XmlSchemaSimpleTypeList-Klasse oder der XmlSchemaSimpleTypeUnion-Klasse Listentypen oder Uniontypen erstellen. Die XmlSchemaSimpleType.Content-Eigenschaft gibt an, ob es sich um eine Einschränkung, eine Liste oder eine Union von einem einfachen Typ handelt.

Im folgenden Codebeispiel ist der Typ des FirstName-Elements der integrierte Typ xs:string. Der Typ des LastName-Elements ist ein benannter einfacher Typ, der eine Einschränkung des integrierten Typs xs:string mit einem Wert des MaxLength-Facets von 20 ist. Der Typ des CustomerId-Attributs ist der integrierte Typ xs:positiveInteger. Das Customer-Element ist ein anonymer komplexer Typ, dessen Partikel eine Sequenz des FirstName-Elements und des LastName-Elements ist und dessen Attribute das CustomerId-Attribut enthalten.

Hinweis:

Die XmlSchemaChoice-Klasse und die XmlSchemaAll-Klasse können auch als Partikel des komplexen Typs zum Replizieren der <xs:choice />-Semantik oder der <xs:all />-Semantik verwendet werden.

' Create the simple type for the LastName element.
Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
lastNameType.Name = "LastNameType"
Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
    New XmlSchemaSimpleTypeRestriction()
lastNameRestriction.BaseTypeName = _
    New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
maxLength.Value = "20"
lastNameRestriction.Facets.Add(maxLength)
lastNameType.Content = lastNameRestriction

' Associate the elements and attributes with their types.
' Built-in type.
firstNameElement.SchemaTypeName = _
    New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' User-defined type.
lastNameElement.SchemaTypeName = _
    New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
' Built-in type.
idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
    "http://www.w3.org/2001/XMLSchema")

' Create the top-level Customer element.
Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
customerElement.Name = "Customer"

' Create an anonymous complex type for the Customer element.
Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
sequence.Items.Add(firstNameElement)
sequence.Items.Add(lastNameElement)
customerType.Particle = sequence

' Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute)

' Set the SchemaType of the Customer element to
' the anonymous complex type created above.
customerElement.SchemaType = customerType
// Create the simple type for the LastName element.
XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
lastNameType.Name = "LastNameType";
XmlSchemaSimpleTypeRestriction lastNameRestriction =
    new XmlSchemaSimpleTypeRestriction();
lastNameRestriction.BaseTypeName =
    new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
maxLength.Value = "20";
lastNameRestriction.Facets.Add(maxLength);
lastNameType.Content = lastNameRestriction;

// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement.SchemaTypeName =
    new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement.SchemaTypeName =
    new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
    "http://www.w3.org/2001/XMLSchema");

// Create the top-level Customer element.
XmlSchemaElement customerElement = new XmlSchemaElement();
customerElement.Name = "Customer";

// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType customerType = new XmlSchemaComplexType();
XmlSchemaSequence sequence = new XmlSchemaSequence();
sequence.Items.Add(firstNameElement);
sequence.Items.Add(lastNameElement);
customerType.Particle = sequence;

// Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute);

// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement.SchemaType = customerType;
// Create the simple type for the LastName element.
XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
lastNameType->Name = "LastNameType";
XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
    gcnew XmlSchemaSimpleTypeRestriction();
lastNameRestriction->BaseTypeName =
    gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
maxLength->Value = "20";
lastNameRestriction->Facets->Add(maxLength);
lastNameType->Content = lastNameRestriction;

// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement->SchemaTypeName =
    gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement->SchemaTypeName =
    gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
    "http://www.w3.org/2001/XMLSchema");

// Create the top-level Customer element.
XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
customerElement->Name = "Customer";

// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
sequence->Items->Add(firstNameElement);
sequence->Items->Add(lastNameElement);
customerType->Particle = sequence;

// Add the CustomerId attribute to the complex type.
customerType->Attributes->Add(idAttribute);

// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement->SchemaType = customerType;

Erstellen und Kompilieren von Schemata

Bisher wurden die untergeordneten Elemente und Attribute, deren jeweilige Typen und das Customer-Element der obersten Ebene speicherintern mithilfe der SOM-API erstellt. Im folgenden Codebeispiel wird das Schemaelement mithilfe der XmlSchema-Klasse erstellt, diesem werden die Elemente und Typen der obersten Ebene mithilfe der XmlSchema.Items-Eigenschaft hinzugefügt, und das gesamte Schema wird mithilfe der XmlSchemaSet-Klasse kompiliert und auf der Konsole ausgegeben.

' Create an empty schema.
Dim customerSchema As XmlSchema = New XmlSchema()
customerSchema.TargetNamespace = "http://www.tempuri.org"

' Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement)
customerSchema.Items.Add(lastNameType)

' Create an XmlSchemaSet to compile the customer schema.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add(customerSchema)
schemaSet.Compile()

For Each schema As XmlSchema In schemaSet.Schemas()
    customerSchema = schema
Next

' Write the complete schema to the Console.
customerSchema.Write(Console.Out)
// Create an empty schema.
XmlSchema customerSchema = new XmlSchema();
customerSchema.TargetNamespace = "http://www.tempuri.org";

// Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement);
customerSchema.Items.Add(lastNameType);

// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add(customerSchema);
schemaSet.Compile();

foreach (XmlSchema schema in schemaSet.Schemas())
{
    customerSchema = schema;
}

// Write the complete schema to the Console.
customerSchema.Write(Console.Out);
// Create an empty schema.
XmlSchema^ customerSchema = gcnew XmlSchema();
customerSchema->TargetNamespace = "http://www.tempuri.org";

// Add all top-level element and types to the schema
customerSchema->Items->Add(customerElement);
customerSchema->Items->Add(lastNameType);

// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
schemaSet->Add(customerSchema);
schemaSet->Compile();

for each (XmlSchema^ schema in schemaSet->Schemas())
{
    customerSchema = schema;
}

// Write the complete schema to the Console.
customerSchema->Write(Console::Out);

Die XmlSchemaSet.Compile-Methode validiert das Kundenschema anhand der Regeln für ein XML-Schema und macht Post-Schema-Compilation-Eigenschaften verfügbar.

Hinweis:

Alle Post-Schema-Compilation-Eigenschaften in der SOM-API weichen vom Post-Schema-Validation-Infoset ab.

Der dem XmlSchemaSet hinzugefügte ValidationEventHandler ist ein Delegat, der die Rückrufmethode ValidationCallback zum Behandeln der Schemavalidierungswarnungen und -fehler aufruft.

Im Folgenden wird das gesamte Codebeispiel dargestellt und das Kundenschema auf der Konsole ausgegeben.

Imports System
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaCreateExample

    Shared Sub Main()

        ' Create the FirstName and LastName elements.
        Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
        firstNameElement.Name = "FirstName"
        Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
        lastNameElement.Name = "LastName"

        ' Create CustomerId attribute.
        Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
        idAttribute.Name = "CustomerId"
        idAttribute.Use = XmlSchemaUse.Required

        ' Create the simple type for the LastName element.
        Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
        lastNameType.Name = "LastNameType"
        Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
            New XmlSchemaSimpleTypeRestriction()
        lastNameRestriction.BaseTypeName = _
            New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
        Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
        maxLength.Value = "20"
        lastNameRestriction.Facets.Add(maxLength)
        lastNameType.Content = lastNameRestriction

        ' Associate the elements and attributes with their types.
        ' Built-in type.
        firstNameElement.SchemaTypeName = _
            New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
        ' User-defined type.
        lastNameElement.SchemaTypeName = _
            New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
        ' Built-in type.
        idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
            "http://www.w3.org/2001/XMLSchema")

        ' Create the top-level Customer element.
        Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
        customerElement.Name = "Customer"

        ' Create an anonymous complex type for the Customer element.
        Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
        Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
        sequence.Items.Add(firstNameElement)
        sequence.Items.Add(lastNameElement)
        customerType.Particle = sequence

        ' Add the CustomerId attribute to the complex type.
        customerType.Attributes.Add(idAttribute)

        ' Set the SchemaType of the Customer element to
        ' the anonymous complex type created above.
        customerElement.SchemaType = customerType

        ' Create an empty schema.
        Dim customerSchema As XmlSchema = New XmlSchema()
        customerSchema.TargetNamespace = "http://www.tempuri.org"

        ' Add all top-level element and types to the schema
        customerSchema.Items.Add(customerElement)
        customerSchema.Items.Add(lastNameType)

        ' Create an XmlSchemaSet to compile the customer schema.
        Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
        schemaSet.Add(customerSchema)
        schemaSet.Compile()

        For Each schema As XmlSchema In schemaSet.Schemas()
            customerSchema = schema
        Next

        ' Write the complete schema to the Console.
        customerSchema.Write(Console.Out)
    End Sub

    Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
        If args.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
        Else
            If args.Severity = XmlSeverityType.Error Then
                Console.Write("ERROR: ")
            End If
        End If
        Console.WriteLine(args.Message)
    End Sub
End Class
using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaCreateExample
{
    static void Main(string[] args)
    {
        // Create the FirstName and LastName elements.
        XmlSchemaElement firstNameElement = new XmlSchemaElement();
        firstNameElement.Name = "FirstName";
        XmlSchemaElement lastNameElement = new XmlSchemaElement();
        lastNameElement.Name = "LastName";

        // Create CustomerId attribute.
        XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
        idAttribute.Name = "CustomerId";
        idAttribute.Use = XmlSchemaUse.Required;

        // Create the simple type for the LastName element.
        XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
        lastNameType.Name = "LastNameType";
        XmlSchemaSimpleTypeRestriction lastNameRestriction =
            new XmlSchemaSimpleTypeRestriction();
        lastNameRestriction.BaseTypeName =
            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
        maxLength.Value = "20";
        lastNameRestriction.Facets.Add(maxLength);
        lastNameType.Content = lastNameRestriction;

        // Associate the elements and attributes with their types.
        // Built-in type.
        firstNameElement.SchemaTypeName =
            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        // User-defined type.
        lastNameElement.SchemaTypeName =
            new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
        // Built-in type.
        idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
            "http://www.w3.org/2001/XMLSchema");

        // Create the top-level Customer element.
        XmlSchemaElement customerElement = new XmlSchemaElement();
        customerElement.Name = "Customer";

        // Create an anonymous complex type for the Customer element.
        XmlSchemaComplexType customerType = new XmlSchemaComplexType();
        XmlSchemaSequence sequence = new XmlSchemaSequence();
        sequence.Items.Add(firstNameElement);
        sequence.Items.Add(lastNameElement);
        customerType.Particle = sequence;

        // Add the CustomerId attribute to the complex type.
        customerType.Attributes.Add(idAttribute);

        // Set the SchemaType of the Customer element to
        // the anonymous complex type created above.
        customerElement.SchemaType = customerType;

        // Create an empty schema.
        XmlSchema customerSchema = new XmlSchema();
        customerSchema.TargetNamespace = "http://www.tempuri.org";

        // Add all top-level element and types to the schema
        customerSchema.Items.Add(customerElement);
        customerSchema.Items.Add(lastNameType);

        // Create an XmlSchemaSet to compile the customer schema.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
        schemaSet.Add(customerSchema);
        schemaSet.Compile();

        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            customerSchema = schema;
        }

        // Write the complete schema to the Console.
        customerSchema.Write(Console.Out);
    }

    static void ValidationCallback(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.Write("WARNING: ");
        else if (args.Severity == XmlSeverityType.Error)
            Console.Write("ERROR: ");

        Console.WriteLine(args.Message);
    }
}
#using <System.Xml.dll>

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

ref class XmlSchemaCreateExample
{
public:

    static void Main()
    {
        // Create the FirstName and LastName elements.
        XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
        firstNameElement->Name = "FirstName";
        XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
        lastNameElement->Name = "LastName";

        // Create CustomerId attribute.
        XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
        idAttribute->Name = "CustomerId";
        idAttribute->Use = XmlSchemaUse::Required;

        // Create the simple type for the LastName element.
        XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
        lastNameType->Name = "LastNameType";
        XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
            gcnew XmlSchemaSimpleTypeRestriction();
        lastNameRestriction->BaseTypeName =
            gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
        maxLength->Value = "20";
        lastNameRestriction->Facets->Add(maxLength);
        lastNameType->Content = lastNameRestriction;

        // Associate the elements and attributes with their types.
        // Built-in type.
        firstNameElement->SchemaTypeName =
            gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        // User-defined type.
        lastNameElement->SchemaTypeName =
            gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
        // Built-in type.
        idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
            "http://www.w3.org/2001/XMLSchema");

        // Create the top-level Customer element.
        XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
        customerElement->Name = "Customer";

        // Create an anonymous complex type for the Customer element.
        XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
        XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
        sequence->Items->Add(firstNameElement);
        sequence->Items->Add(lastNameElement);
        customerType->Particle = sequence;

        // Add the CustomerId attribute to the complex type.
        customerType->Attributes->Add(idAttribute);

        // Set the SchemaType of the Customer element to
        // the anonymous complex type created above.
        customerElement->SchemaType = customerType;

        // Create an empty schema.
        XmlSchema^ customerSchema = gcnew XmlSchema();
        customerSchema->TargetNamespace = "http://www.tempuri.org";

        // Add all top-level element and types to the schema
        customerSchema->Items->Add(customerElement);
        customerSchema->Items->Add(lastNameType);

        // Create an XmlSchemaSet to compile the customer schema.
        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
        schemaSet->Add(customerSchema);
        schemaSet->Compile();

        for each (XmlSchema^ schema in schemaSet->Schemas())
        {
            customerSchema = schema;
        }

        // Write the complete schema to the Console.
        customerSchema->Write(Console::Out);
    }

    static void ValidationCallback(Object^ sender, ValidationEventArgs^ args)
    {
        if (args->Severity == XmlSeverityType::Warning)
            Console::Write("WARNING: ");
        else if (args->Severity == XmlSeverityType::Error)
            Console::Write("ERROR: ");

        Console::WriteLine(args->Message);
    }
};

int main()
{
    XmlSchemaCreateExample::Main();
    return 0;
}

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://www.tempuri.org" targetNamespace="http://www.tempuri.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="Customer">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="FirstName" type="xs:string" />
            <xs:element name="LastName" type="tns:LastNameType" />
         </xs:sequence>
         <xs:attribute name="CustomerId" type="xs:positiveInteger" use="required" />
      </xs:complexType>
   </xs:element>
   <xs:simpleType name="LastNameType">
      <xs:restriction base="xs:string">
         <xs:maxLength value="20" />
      </xs:restriction>
   </xs:simpleType>
</xs:schema>

Siehe auch

Konzepte

Übersicht über das XML-Schemaobjektmodell (SOM)

Lesen und Schreiben von XML-Schemata

Durchlaufen von XML-Schemata

Bearbeiten von XML-Schemata

Einfügen oder Importieren von XML-Schemata

"XmlSchemaSet" zur Kompilierung von Schemata

Post-Schema-Compilation Infoset