周遊 XML 結構描述

使用結構描述物件模型 (SOM) API 周遊 XML 結構描述,可以提供對儲存於 SOM 之項目、屬性及型別的存取權。 周遊載入到 SOM 的 XML 結構描述也是使用 SOM API 編輯 XML 結構描述的第一步。

周遊 XML 結構描述

XmlSchema 類別的下列屬性可以提供對加入至 XML 結構描述之所有全域項目集合的存取權。

屬性 儲存於集合或陣列中的物件型別
Elements XmlSchemaElement
Attributes XmlSchemaAttribute
AttributeGroups XmlSchemaAttributeGroup
Groups XmlSchemaGroup
Includes XmlSchemaExternalXmlSchemaIncludeXmlSchemaImportXmlSchemaRedefine
Items XmlSchemaObject (可提供對所有全域層級項目、屬性及型別的存取權)。
Notations XmlSchemaNotation
SchemaTypes XmlSchemaTypeXmlSchemaSimpleTypeXmlSchemaComplexType
UnhandledAttributes XmlAttribute (提供對不屬於結構描述命名空間之屬性的存取權)

注意

上述表格中列出的所有屬性 (除 Items 屬性之外),都是後結構描述編譯資訊集 (PSCI) 屬性,僅在編譯結構描述之後才可用。 Items 屬性是前結構描述編譯屬性,其可在編譯結構描述之前使用,以存取及編輯所有全域層級項目、屬性及型別。

UnhandledAttributes 屬性提供對不屬於結構描述命名空間之所有屬性的存取權。 結構描述處理器不會處理這些屬性。

接下來的程式碼範例會示範如何周遊建置 XML 結構描述主題中建立的客戶結構描述。 該程式碼範例示範如何使用上述集合周遊結構描述,並將結構描述中的所有項目和屬性寫入主控台。

該範例會按下列步驟周遊客戶結構描述。

  1. 將客戶結構描述加入至新 XmlSchemaSet 物件,然後編譯它。 讀取或編譯結構描述時遇到的任何結構描述驗證警告及錯誤,都會由 ValidationEventHandler 委派處理。

  2. 藉由重複處理 XmlSchema 屬性,從 XmlSchemaSet 擷取已編譯的 Schemas 物件。 因為已編譯結構描述,因此可存取後結構描述編譯資訊集 (PSCI) 屬性。

  3. 重複處理後結構描述編譯 XmlSchemaElement 集合之 Values 集合中的每個 XmlSchema.Elements,會將每個項目的名稱寫入主控台。

  4. 使用 Customer 類別,取得 XmlSchemaComplexType 項目的複雜型別。

  5. 如果複雜型別具有任何屬性,請取得 IDictionaryEnumerator 來列舉每個 XmlSchemaAttribute,並將其名稱寫入主控台。

  6. 使用 XmlSchemaSequence 類別,取得複雜型別的順序物件。

  7. 重複處理 XmlSchemaElement 集合中的每個 XmlSchemaSequence.Items,會將每個項目子系的名稱寫入主控台。

下列是完整的程式碼範例。

#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

如果 XmlSchemaElement.ElementSchemaType 屬性是使用者定義的簡單型別或複雜型別,則其可以為 XmlSchemaSimpleTypeXmlSchemaComplexType。 如果其為 W3C XML 結構描述建議事項中定義的其中一個內建資料型別,則也可以為 XmlSchemaDatatype。 在客戶結構描述中,ElementSchemaType 項目的 CustomerXmlSchemaComplexTypeFirstNameLastName 項目為 XmlSchemaSimpleType

建置 XML 結構描述主題中的程式碼範例使用 XmlSchemaComplexType.Attributes 集合,將屬性 CustomerId 加入至 Customer 項目。 這是前結構描述編譯屬性。 對應的後結構描述編譯資訊集屬性為 XmlSchemaComplexType.AttributeUses 集合,其保留複雜型別的所有屬性,包括透過型別衍生繼承的那些屬性。

另請參閱