共用方式為


周遊 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

XmlSchemaType, XmlSchemaSimpleType, XmlSchemaComplexType

UnhandledAttributes

XmlAttribute (提供對不屬於結構描述命名空間之屬性的存取權)

注意事項注意事項

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

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

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

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

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

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

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

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

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

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

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

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

Imports System
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
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);
    }
}
#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;
};

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

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

請參閱

概念

XML 結構描述物件模型概觀

讀取及寫入 XML 結構描述

建置 XML 結構描述

編輯 XML 結構描述

併入或匯入 XML 結構描述

用於結構描述編譯的 XmlSchemaSet

後結構描述編譯資訊集