Click here to Skip to main content
11,802,222 members (52,414 online)
Click here to Skip to main content

Parsing XSD Schema with SOM

, 13 Oct 2008 CPOL 34.2K 993 25
Rate this:
Please Sign up or sign in to vote.
Pull metadata from a schema or generate XML mappers


This article shows how to use a Schema Object Model(SOM) navigator as an arbitrary schema. This is useful when you have a schema and want to generate your own mapping or want to build a custom XML generator or XSD based code generator.

Using the Code

First read schema and "compile", this will validate the schema.

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;

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

Start traversing at the highest level...

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

    foreach (XmlSchemaElement elem in

Only an element can contain attributes... so it's handled in a little special way:

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


Process the Choice & Sequence elements generically (this handles the type casting required):

private static void ProcessSequence(XmlSchemaSequence sequence)

private static void ProcessChoice(XmlSchemaChoice choice)

The Schema objects are processed generically, but uniformly:

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

Points of Interest

There's a lot of type checking and casting. That's inherent in the data model used in the SOM. It's not a strongly typed data structure. So you have to check, cast, then use. It would be an interesting exercise to generate a strongly typed XSD model, but I'll leave that for another article.


  • 2008-10-10: Article created


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...

Comments and Discussions

QuestionMessage Automatically Removed Pin
26-Aug-14 0:44
memberDavid Harrison26-Aug-14 0:44 
QuestionLinqToXsd Update Pin
CodingBruce6-May-12 2:31
memberCodingBruce6-May-12 2:31 
QuestionThanks a lot! Pin
DanielLey18-Mar-12 12:50
memberDanielLey18-Mar-12 12:50 
Questionparsing XSD schema Pin
zinazsh22-Nov-11 11:06
memberzinazsh22-Nov-11 11:06 
AnswerRe: parsing XSD schema Pin
CodingBruce23-Nov-11 1:23
memberCodingBruce23-Nov-11 1:23 
QuestionLinq2Xsd makes this silly Pin
CodingBruce23-Sep-11 13:38
memberCodingBruce23-Sep-11 13:38 
GeneralThe lights go on... Pin
Ger Hayden6-Jun-11 21:09
memberGer Hayden6-Jun-11 21:09 
QuestionHow to extract Description that is within annotation tag?Please help! [modified] Pin
abhinavchowdhary18-May-11 10:58
memberabhinavchowdhary18-May-11 10:58 
AnswerRe: How to extract Description that is within annotation tag?Please help! [modified] Pin
RayD23-Sep-11 13:25
memberRayD23-Sep-11 13:25 
QuestionProblem with nested <xs:group> Pin
akothoor12-Apr-11 5:38
memberakothoor12-Apr-11 5:38 
GeneralSchema w/LinqToXsd Pin
CodingBruce4-Dec-10 10:07
memberCodingBruce4-Dec-10 10:07 
I use all the code-generators on a regular basis for my day job. I finally found on that could generate a strongly typed data model from the xsd.xsd (schema schema). So I will be posting another article on that soon, I'll link forward when it's done

Generalthanks Pin
Member 112416822-Jun-09 0:25
memberMember 112416822-Jun-09 0:25 
GeneralThank you Pin
Member 238152731-May-09 20:16
memberMember 238152731-May-09 20:16 
GeneralThanks Pin
John Irwin29-Oct-08 1:26
memberJohn Irwin29-Oct-08 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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