Export (0) Print
Expand All

Traversing XML Schemas

Traversing an XML schema using the Schema Object Model (SOM) API provides access to the elements, attributes, and types stored in the SOM. Traversing an XML schema loaded into the SOM is also the first step in editing an XML schema using the SOM API.

The following properties of the XmlSchema class provide access to the collection of all global items added to the XML schema.

Property

Object type stored in the collection or array

Elements

XmlSchemaElement

Attributes

XmlSchemaAttribute

AttributeGroups

XmlSchemaAttributeGroup

Groups

XmlSchemaGroup

Includes

XmlSchemaExternal , XmlSchemaInclude, XmlSchemaImport, or XmlSchemaRedefine

Items

XmlSchemaObject (provides access to all global level elements, attributes, and types).

Notations

XmlSchemaNotation

SchemaTypes

XmlSchemaType , XmlSchemaSimpleType, XmlSchemaComplexType

UnhandledAttributes

XmlAttribute (provides access to attributes that do not belong to the schema namespace)

Note Note

All of the properties listed in the table above, except for the Items property, are Post-Schema-Compilation-Infoset (PSCI) properties that are not available until the schema has been compiled. The Items property is a pre-schema-compilation property that can be used before the schema has been compiled to access and edit all global level elements, attributes, and types.

The UnhandledAttributes property provides access to all the attributes that do not belong to the schema namespace. These attributes are not processed by the schema processor.

The code example that follows demonstrates traversing the customer schema created in the Building XML Schemas topic. The code example demonstrates traversing the schema using the collections described above and writes all the elements and attributes in the schema to the console.

The sample traverses the customer schema in the following steps.

  1. Adds the customer schema to a new XmlSchemaSet object and then compiles it. Any schema validation warnings and errors encountered reading or compiling the schema are handled by the ValidationEventHandler delegate.

  2. Retrieves the compiled XmlSchema object from the XmlSchemaSet by iterating over the Schemas property. Because the schema is compiled, Post-Schema-Compilation-Infoset (PSCI) properties are accessible.

  3. Iterates over each XmlSchemaElement in the Values collection of the post-schema-compilation XmlSchema.Elements collection writing the name of each element to the console.

  4. Gets the complex type of the Customer element using the XmlSchemaComplexType class.

  5. If the complex type has any attributes, gets an IDictionaryEnumerator to enumerate over each XmlSchemaAttribute and writes its name to the console.

  6. Gets the sequence particle of the complex type using the XmlSchemaSequence class.

  7. Iterates over each XmlSchemaElement in the XmlSchemaSequence.Items collection writing the name of each child element to the console.

The following is the complete code example.

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);
    }
}

The XmlSchemaElement.ElementSchemaType property can be XmlSchemaSimpleType, or XmlSchemaComplexType if it is a user-defined simple type or a complex type. It can also be XmlSchemaDatatype if it is one of the built-in datatypes defined in the W3C XML Schema Recommendation. In the customer schema, the ElementSchemaType of the Customer element is XmlSchemaComplexType, and the FirstName and LastName elements are XmlSchemaSimpleType.

The code example in the Building XML Schemas topic used the XmlSchemaComplexType.Attributes collection to add the attribute CustomerId to the Customer element. This is a pre-schema-compilation property. The corresponding Post-Schema-Compilation-Infoset property is the XmlSchemaComplexType.AttributeUses collection, which holds all the attributes of the complex type, including the ones that are inherited through type derivation.

Show:
© 2014 Microsoft