Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / XML

Parsing XSD Schema with SOM

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
13 Oct 2008CPOL 74.6K   1.3K   25  
Pull metadata from a schema or generate XML mappers
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Collections;

namespace UnderstandingSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            TraverseSOM(args[0]);
        }

        private static XmlSchema
        ReadAndCompileSchema(string fileName)
        {
            XmlTextReader tr = new XmlTextReader(fileName,
                new NameTable());

            // The Read method will throw errors encountered
            // on parsing the schema
            XmlSchema schema = XmlSchema.Read(tr,
                new ValidationEventHandler(ValidationCallbackOne));
            tr.Close();

            XmlSchemaSet xset = new XmlSchemaSet();
            xset.Add(schema);

            xset.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);

            // The Compile method will throw errors 
            // encountered on compiling the schema
            xset.Compile();

            return schema;
        }

        public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
        {
            Console.WriteLine("Exception Severity: " + args.Severity);
            Console.WriteLine(args.Message);
        }

        private static void TraverseSOM(string xsfFilename)
        {
            XmlSchema custSchema = ReadAndCompileSchema(xsfFilename);

            foreach (XmlSchemaElement elem in
                         custSchema.Elements.Values)
            {
                ProcessElement(elem);
            }
        }

        private static void ProcessElement(XmlSchemaElement elem)
        {
            Console.WriteLine("Element: {0}", elem.Name);

            if (elem.ElementSchemaType is XmlSchemaComplexType)
            {
                XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;

                foreach (DictionaryEntry obj in ct.AttributeUses)
                    Console.WriteLine("Attribute: {0}  ", (obj.Value as XmlSchemaAttribute).Name);

                ProcessSchemaObject(ct.ContentTypeParticle);
            }
        }

        private static void ProcessSequence(XmlSchemaSequence sequence)
        {
            Console.WriteLine("Sequence");
            ProcessItemCollection(sequence.Items);
        }

        private static void ProcessChoice(XmlSchemaChoice choice)
        {
            Console.WriteLine("Choice");
            ProcessItemCollection(choice.Items);
        }

        private static void ProcessItemCollection(XmlSchemaObjectCollection objs)
        {
            foreach (XmlSchemaObject obj in objs)
                ProcessSchemaObject(obj);
        }

        private static void ProcessSchemaObject(XmlSchemaObject obj)
        {
            if (obj is XmlSchemaElement)
                ProcessElement(obj as XmlSchemaElement);
            if (obj is XmlSchemaChoice)
                ProcessChoice(obj as XmlSchemaChoice);
            if (obj is XmlSchemaSequence)
                ProcessSequence(obj as XmlSchemaSequence);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Big Company
United States United States
My professional career began as a developer fixing bugs on Microsoft Word97 and I've been fixing bad habits ever since. Now I do R&D work writing v1 line of business applications mostly in C#/.Net.

I've been an avid pilot/instructor for 13+ years, I've built two airplanes and mostly fly gliders now for fun. I commute in an all-electric 1986 BMW 325 conversion.

I'd like to get back to my academic roots of programming 3D analysis applications to organize complex systems.

Comments and Discussions