Click here to Skip to main content
11,705,208 members (48,870 online)
Click here to Skip to main content
Add your own
alternative version

Parsing XSD Schema with SOM

, 13 Oct 2008 CPOL 33.6K 977 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)

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

            XmlSchemaSet xset = new XmlSchemaSet();

            xset.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);

            // The Compile method will throw errors 
            // encountered on compiling the schema

            return schema;

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

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

            foreach (XmlSchemaElement elem in

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


        private static void ProcessSequence(XmlSchemaSequence sequence)

        private static void ProcessChoice(XmlSchemaChoice choice)

        private static void ProcessItemCollection(XmlSchemaObjectCollection objs)
            foreach (XmlSchemaObject obj in objs)

        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.


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


About the Author

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 13 Oct 2008
Article Copyright 2008 by CodingBruce
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid