遍历 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 属性以外,都是架构后Compilation-Infoset(PSCI)属性,这些属性在架构编译后才可用。 该 Items 属性是一个预架构编译属性,可在编译架构之前使用该属性来访问和编辑所有全局级别元素、属性和类型。

UnhandledAttributes 属性提供对不属于架构命名空间的所有属性的访问权限。 架构处理器不会处理这些属性。

下面的代码示例演示如何遍历 在生成 XML 架构 主题中创建的客户架构。 该代码示例演示如何使用上述集合遍历架构,并将架构中的所有元素和属性写入控制台。

此示例在以下步骤中遍历客户架构。

  1. 将客户架构添加到新 XmlSchemaSet 对象,然后对其进行编译。 任何架构验证警告和错误在读取或编译架构时由委托ValidationEventHandler处理。

  2. 通过循环访问XmlSchema属性从XmlSchemaSet中检索已Schemas编译的对象。 由于架构已编译,因此可以访问架构后Compilation-Infoset (PSCI) 属性。

  3. 循环访问后架构编译XmlSchemaElement集合的Values集合中的每个XmlSchema.Elements,将每个元素的名称写入控制台。

  4. 使用Customer类获取XmlSchemaComplexType元素的复杂类型。

  5. 如果复杂类型具有任何属性,则获取一个 IDictionaryEnumerator 来枚举每个 XmlSchemaAttribute 属性,并将其名称写入控制台。

  6. 使用 XmlSchemaSequence 类获取复杂类型的序列粒子。

  7. 循环访问 XmlSchemaElement 集合中的每个 XmlSchemaSequence.Items 元素,将每个子元素的名称写入控制台。

下面是完整的代码示例。

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: {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: {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: {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 属性可以是 XmlSchemaSimpleType,或者 XmlSchemaComplexType 它是用户定义的简单类型或复杂类型。 如果是 W3C XML 架构建议中定义的内置数据类型之一,它也可以 XmlSchemaDatatype。 在客户架构中,ElementSchemaTypeCustomer元素为XmlSchemaComplexType,并且FirstName元素LastNameXmlSchemaSimpleType

Building XML Schemas 主题中的代码示例使用XmlSchemaComplexType.Attributes集合将属性CustomerId添加到Customer元素。 这是预架构编译属性。 相应的架构后Compilation-Infoset 属性是 XmlSchemaComplexType.AttributeUses 集合,该集合包含复杂类型的所有属性,包括通过类型派生继承的属性。

另请参阅