Delen via


Xml-schema's doorlopen

Doorkruisen van een XML-schema met behulp van de SOM-API (Schema Object Model) biedt toegang tot de elementen, kenmerken en typen die zijn opgeslagen in de SOM. Het doorlopen van een XML-schema dat in de SOM is geladen, is ook de eerste stap bij het bewerken van een XML-schema met behulp van de SOM-API.

Een XML-schema doorlopen

De volgende eigenschappen van de XmlSchema klasse bieden toegang tot de verzameling van alle globale items die zijn toegevoegd aan het XML-schema.

Eigenschappen Objecttype dat is opgeslagen in de verzameling of matrix
Elements XmlSchemaElement
Attributes XmlSchemaAttribute
AttributeGroups XmlSchemaAttributeGroup
Groups XmlSchemaGroup
Includes XmlSchemaExternal, , XmlSchemaIncludeof XmlSchemaImportXmlSchemaRedefine
Items XmlSchemaObject (biedt toegang tot alle elementen, kenmerken en typen op globaal niveau).
Notations XmlSchemaNotation
SchemaTypes XmlSchemaType, , XmlSchemaSimpleTypeXmlSchemaComplexType
UnhandledAttributes XmlAttribute (biedt toegang tot kenmerken die niet tot de schemanaamruimte behoren)

Notitie

Alle eigenschappen die worden vermeld in de bovenstaande tabel, met uitzondering van de Items eigenschap, zijn psci-eigenschappen (Post-Schema-Compilation-Infoset) die pas beschikbaar zijn als het schema is gecompileerd. De Items eigenschap is een eigenschap vooraf schemacompilatie die kan worden gebruikt voordat het schema is gecompileerd om alle elementen, kenmerken en typen op globaal niveau te openen en te bewerken.

De UnhandledAttributes eigenschap biedt toegang tot alle kenmerken die niet tot de schemanaamruimte behoren. Deze kenmerken worden niet verwerkt door de schemaprocessor.

In het volgende codevoorbeeld ziet u hoe u het klantschema doorkruist dat is gemaakt in het onderwerp Xml-schema's bouwen. In het codevoorbeeld ziet u hoe u het schema doorkruist met behulp van de hierboven beschreven verzamelingen en schrijft alle elementen en kenmerken in het schema naar de console.

Het voorbeeld doorkruist het klantschema in de volgende stappen.

  1. Voegt het klantschema toe aan een nieuw XmlSchemaSet object en compileert het vervolgens. Eventuele schemavalidatiewaarschuwingen en fouten die zijn opgetreden bij het lezen of compileren van het schema, worden verwerkt door de ValidationEventHandler gemachtigde.

  2. Haalt het gecompileerde XmlSchema object op van de XmlSchemaSet eigenschap door de Schemas eigenschap te herhalen. Omdat het schema is gecompileerd, zijn de eigenschappen Post-Schema-Compilation-Infoset (PSCI) toegankelijk.

  3. Herhaalt elk XmlSchemaElement element in de Values verzameling van de verzameling na schemacompilatie XmlSchema.Elements die de naam van elk element naar de console schrijft.

  4. Hiermee haalt u het complexe type van het Customer element op met behulp van de XmlSchemaComplexType klasse.

  5. Als het complexe type kenmerken heeft, krijgt u een IDictionaryEnumerator overzicht van elk XmlSchemaAttribute kenmerk en schrijft u de naam ervan naar de console.

  6. Haalt het sequentiedeeltje van het complexe type op met behulp van de XmlSchemaSequence klasse.

  7. Herhaalt elke XmlSchemaElement in de XmlSchemaSequence.Items verzameling de naam van elk onderliggend element naar de console.

Hier volgt het volledige codevoorbeeld.

#using <System.Xml.dll>

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

ref class XmlSchemaTraverseExample
{
public:

    static void Main()
    {
        // Add the customer schema to a new XmlSchemaSet and compile it.
        // Any schema validation warnings and errors encountered reading or 
        // compiling the schema are handled by the ValidationEventHandler delegate.
        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
        schemaSet->Add("http://www.tempuri.org", "customer.xsd");
        schemaSet->Compile();

        // Retrieve the compiled XmlSchema object from the XmlSchemaSet
        // by iterating over the Schemas property.
        XmlSchema^ customerSchema = nullptr;
        for each (XmlSchema^ schema in schemaSet->Schemas())
        {
            customerSchema = schema;
        }

        // Iterate over each XmlSchemaElement in the Values collection
        // of the Elements property.
        for each (XmlSchemaElement^ element in customerSchema->Elements->Values)
        {

            Console::WriteLine("Element: {0}", element->Name);

            // Get the complex type of the Customer element.
            XmlSchemaComplexType^ complexType = dynamic_cast<XmlSchemaComplexType^>(element->ElementSchemaType);

            // If the complex type has any attributes, get an enumerator 
            // and write each attribute name to the console.
            if (complexType->AttributeUses->Count > 0)
            {
                IDictionaryEnumerator^ enumerator =
                    complexType->AttributeUses->GetEnumerator();

                while (enumerator->MoveNext())
                {
                    XmlSchemaAttribute^ attribute =
                        dynamic_cast<XmlSchemaAttribute^>(enumerator->Value);

                    Console::WriteLine("Attribute: {0}", attribute->Name);
                }
            }

            // Get the sequence particle of the complex type.
            XmlSchemaSequence^ sequence = dynamic_cast<XmlSchemaSequence^>(complexType->ContentTypeParticle);

            // Iterate over each XmlSchemaElement in the Items collection.
            for each (XmlSchemaElement^ childElement in sequence->Items)
            {
                Console::WriteLine("Element: {0}", childElement->Name);
            }
        }
    }

    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()
{
    XmlSchemaTraverseExample::Main();
    return 0;
};
using System;
using System.Collections;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaTraverseExample
{
    static void Main()
    {
        // Add the customer schema to a new XmlSchemaSet and compile it.
        // Any schema validation warnings and errors encountered reading or
        // compiling the schema are handled by the ValidationEventHandler delegate.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
        schemaSet.Add("http://www.tempuri.org", "customer.xsd");
        schemaSet.Compile();

        // Retrieve the compiled XmlSchema object from the XmlSchemaSet
        // by iterating over the Schemas property.
        XmlSchema customerSchema = null;
        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            customerSchema = schema;
        }

        // Iterate over each XmlSchemaElement in the Values collection
        // of the Elements property.
        foreach (XmlSchemaElement element in customerSchema.Elements.Values)
        {

            Console.WriteLine("Element: {0}", element.Name);

            // Get the complex type of the Customer element.
            XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;

            // If the complex type has any attributes, get an enumerator
            // and write each attribute name to the console.
            if (complexType.AttributeUses.Count > 0)
            {
                IDictionaryEnumerator enumerator =
                    complexType.AttributeUses.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    XmlSchemaAttribute attribute =
                        (XmlSchemaAttribute)enumerator.Value;

                    Console.WriteLine("Attribute: {0}", attribute.Name);
                }
            }

            // Get the sequence particle of the complex type.
            XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;

            // Iterate over each XmlSchemaElement in the Items collection.
            foreach (XmlSchemaElement childElement in sequence.Items)
            {
                Console.WriteLine("Element: {0}", childElement.Name);
            }
        }
    }

    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);
    }
}
Imports System.Collections
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaTraverseExample

    Shared Sub Main()

        ' Add the customer schema to a new XmlSchemaSet and compile it.
        ' Any schema validation warnings and errors encountered reading or 
        ' compiling the schema are handled by the ValidationEventHandler delegate.
        Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
        schemaSet.Add("http://www.tempuri.org", "customer.xsd")
        schemaSet.Compile()

        ' Retrieve the compiled XmlSchema object from the XmlSchemaSet
        ' by iterating over the Schemas property.
        Dim customerSchema As XmlSchema = Nothing
        For Each schema As XmlSchema In schemaSet.Schemas()
            customerSchema = schema
        Next

        ' Iterate over each XmlSchemaElement in the Values collection
        ' of the Elements property.
        For Each element As XmlSchemaElement In customerSchema.Elements.Values

            Console.WriteLine("Element: {0}", element.Name)

            ' Get the complex type of the Customer element.
            Dim complexType As XmlSchemaComplexType = CType(element.ElementSchemaType, XmlSchemaComplexType)

            ' If the complex type has any attributes, get an enumerator 
            ' and write each attribute name to the console.
            If complexType.AttributeUses.Count > 0 Then

                Dim enumerator As IDictionaryEnumerator = _
                    complexType.AttributeUses.GetEnumerator()

                While enumerator.MoveNext()

                    Dim attribute As XmlSchemaAttribute = _
                        CType(enumerator.Value, XmlSchemaAttribute)

                    Console.WriteLine("Attribute: {0}", Attribute.Name)
                End While
            End If

            ' Get the sequence particle of the complex type.
            Dim sequence As XmlSchemaSequence = CType(complexType.ContentTypeParticle, XmlSchemaSequence)

            For Each childElement As XmlSchemaElement In sequence.Items
                Console.WriteLine("Element: {0}", childElement.Name)
            Next
        Next

    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

De XmlSchemaElement.ElementSchemaType eigenschap kan zijn XmlSchemaSimpleType, of XmlSchemaComplexType als het een door de gebruiker gedefinieerd eenvoudig type of een complex type is. Het kan ook zijn XmlSchemaDatatype als het een van de ingebouwde gegevenstypen is die zijn gedefinieerd in de W3C XML-schemaaanbeveling. In het klantschema is XmlSchemaComplexTypehet van het CustomerElementSchemaType element en de FirstNameLastName elementen.XmlSchemaSimpleType

In het codevoorbeeld in het onderwerp Xml-schema's bouwen is de XmlSchemaComplexType.Attributes verzameling gebruikt om het kenmerk CustomerId toe te voegen aan het Customer element. Dit is een pre-schemacompilatie-eigenschap. De bijbehorende eigenschap Post-Schema-Compilation-Infoset is de XmlSchemaComplexType.AttributeUses verzameling, die alle kenmerken van het complexe type bevat, inclusief de kenmerken die worden overgenomen via typeontzetting.

Zie ook