Click here to Skip to main content
14,032,238 members
Click here to Skip to main content
Add your own
alternative version


112 bookmarked
Posted 1 Oct 2004

An XSD to .NET Language Code Class Generator that Adds Real Punch to Microsoft's XSD Tool

, 29 Mar 2007
Rate this:
Please Sign up or sign in to vote.
This tool extends the Microsoft XSD tool to provide quality and robust auto-generated code that is easy to use.

Table of Contents


In document-centric environments where data is persisted as XML documents, the need often arises to provide business logic at various stages during the processing of these documents. One way to do this is to use the System.Xml.XmlDocument class to 'front' the XML document and provide a means to modify the data. However, XmlDocument essentially models the XML structure of the document, and not the data structure of the document itself. Another way to do this would be to provide a data-access layer with classes that model the actual data structure and content itself, and provide serialization support to read and write the data to XML streams.

The advantages of the second approach are significant to the code developer and maintainer:

  • The classes can be re-used in the context of other data composition classes.
  • The developer works directly with classes and data types that are mostly domain-based, which is semantically easier to understand, more concise to code, and usually the code is a lot easier to read and maintain.
  • The code is usually easier to update as the data definitions change.
  • Visual Studio automatically provides intellisense prompting for these data types, so coding is faster and less error prone.

The mapping of W3C XML schemas (XSDs) to code classes and class attributes, although complex, is consistent, and the automated generation of code is entirely possible and has been provided as a standard set of functionality in Visual Studio. Bundled with Visual Studio is a utility called XSD.EXE, which amongst other functionality, can generate language code from XSDs. The utility provides a console-based application to generate both C#.NET and VB.NET code.

This functionality is provided for the user to generate his/her own code classes, but the functionality behind this utility is also used in Visual Studio to generate serialization map classes for ASP.NET web-services.

If the XSD.EXE generated classes are viewed as data-access classes, looking at the code, one realizes that the code is fairly complex to use and encapsulation is not really provided as all attributes are defined as public fields. In fact, the code, although simple, is quite ugly, and does not conform to many of the practices applied in the production of robust application code.

The purpose of this article is to demonstrate how the XSD tool output code can be modified to provide better encapsulation, robustness, and ease-of-use.


There are a few approaches to 'upgrading' the output of the XSD tool:

  • By using the System.Reflection namespace functionality to obtain the definitions of the classes, and applying a code template generator to generate a set of new classes that either 'wrap' the generated classes, or replace them completely. The advantage of this is, the template can provide specific and complex application code additions (that can be provided by users at runtime!) to generate the new code classes. The disadvantage is that a template must be produced for every target code language. There are a few examples of these on the Internet, some doing the job programmatically and using C# and VB language templates, others using XSL style-sheets to perform a similar job.
  • The other, more versatile method is to create a CodeDOM of the XSD using the same functionality that the XSD tool uses, and then to modify the CodeDOM to provide the desired output. The advantage of this route is that code can, in theory, be generated for any supported .NET language, directly from the CodeDOM. The main disadvantage is that providing language code constructs at the CodeDOM level is very tedious, complex, and error-prone (CodeDOM programming reminds me of assembly-programming - you don't get a lot of bang for your buck!). Currently, there is a distinct lack of real ICodeParser implementations in .NET (including those found in the Rotor and Mono projects) that can generate more than just interface code. This means that currently you can only add code realistically via the CodeDOM route. (ICodeParser implementers will read source-code and produce CodeDOM program graphs. This provides a means of writing code in one .NET language, easily converting it into a CodeDOM, which can then be used to generate code in any of the other .NET languages.)

In this article, I describe how the CodeXS tool essentially follows the second approach. The essence of the CodeXS code generation functionality is described in this MSDN article:

A tool which provides some of the enhanced functionality available in CodeXS can also be downloaded here:


Code-XS adds the following features to the code generated by XSD.EXE:

CodeXS is Built using an Extensible Architecture

The basis for generating code is to produce a CodeDOM from a schema (XSD), and this can be done as follows:

public static CodeNamespace Process(string xsdFile, string targetNamespace)
    // Load the XmlSchema and its collection
    XmlSchema xsd;
    using(FileStream fs = new FileStream(xsdFile, FileMode.Open))
        xsd = XmlSchema.Read(fs, null);
    XmlSchemas schemas = new XmlSchemas();
    // Create the importer for these schemas
    XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
    // System.CodeDom namespace for the XmlCodeExporter to put classes in
    CodeNamespace ns = new CodeNamespace(targetNamespace);
    XmlCodeExporter exporter = new XmlCodeExporter(ns);
    // Iterate schema top-level elements and export code for each
    foreach(XmlSchemaElement element in xsd.Elements.Values)
        // Import the mapping first
        XmlTypeMapping mapping = 
        // Export the code finally

The CodeXS tool provides the basis for reading XML schemas and generating the CodeDOM for the schema classes. The tool then provides a facility to hook up 3rd party code modifier assemblies that provide implementers of a tool-defined ICodeModifier interface. The CodeDOM is then passed to these code modifiers in a structured fashion, and these implementers actually provide the code extensions to the CodeDOM generated by the XSD tool. Once these code implementers have completed, the tool then splits up the CodeDOM into separate CodeDOMs for each schema file (in the case of included schemas), and then generates the code output for each file.

The CodeXS generator exists as a class library. It was decided to invest effort in providing the functionality as an online tool. The CodeXS generator uses an assembly containing a standard set of code modifiers (appropriately called 'StandardCodeModifier.dll') and fronted by an ASP.NET web-service named CodeXS. The CodeXS online tool is an ASP.NET client that is serviced by the CodeXS web-service. The online tool can be found here:

Corrected Use of Elements With Reserved-Keyword Type Names

Some schema definitions define elements or types which are reserved keywords or type names. Global Justice XML (GJXML) defines a schema complex type to wrap textual data called 'string'.

It is possible to use 'string' as a class name in your code as follows, for C#:

public class @string

and for VB:

Public Class [string]
End Class

The XSD tool code generation does provide this class definition, but produces code that does not compile correctly in some cases. CodeXS corrects this error and also extends the functionality to provide for collections of objects of this class. CodeXS supports the redefinition of most reserved types defined by .NET, in this way.

Support for Multi-Included Schemas Using Relative schemaLocation Directory Specifiers

CodeXS provides for the implicit inclusion of all included schemas referenced through the schemaLocation attribute in the target schema root element, and recursively, also those included in the included schemas. The only constraint is that the target schema path is regarded as the root location (either URL or directory path) for all the included schemas, and that their schemaLocations are specified as relative paths. This is true in the majority of published schemas.

Support for Single or Multi-Namespace, Multi-File Schemas

CodeXS will correctly manage the recursive inclusion of all schemas which have a target namespace different from the target schema. CodeXS will also correctly manage the correct XmlSchema object generation for multiple schema-files sharing the same target namespace, by iteratively building up the schema from the source schema files. This is managed in the following method:

private XmlSchemas IncludeSchemas(XmlSchema Parent, Uri SourceUri, 
                           XmlSchemas Schemas, Hashtable AddedSchemas)
    foreach(XmlSchemaExternal externalSchema in Parent.Includes)
            Uri schemaUri = new Uri(SourceUri, externalSchema.SchemaLocation);
            string uriPath = this.GetUriPath(schemaUri);
            XmlSchema schema = this.ReadSchema(uriPath);
            if(AddedSchemas[uriPath] == null)
                if(Schemas[schema.TargetNamespace] != null)
                    XmlSchema compSchema = Schemas[schema.TargetNamespace];
                    foreach(XmlSchemaObject schemaObj in schema.Items)
                        catch { /*ignore*/ }
                AddedSchemas[uriPath] = schema;
                this.IncludeSchemas(schema, schemaUri, Schemas, AddedSchemas);
        catch { /*ignore*/ }
    return Schemas;

Multi-File Code Generation Where Each Schema File Has a Corresponding Code File

Initially, CodeXS supported the XSD tool default and generated one code file for the entire schema set.

The generated file size is over 3 MB in both the C# and VB cases. Editing these files in Visual Studio is difficult enough in the C# editor - in the VB editor, this was just about impossible as the editor became so slow that interaction was non-existent. It was decided to divide the file according to the location of the element and type definitions in the schema files, and to produce code files that effectively corresponded to the schema files in this way. In most cases, the file size problem is solved. An added benefit is that the definitions are nicely partitioned, especially if the schema designer did the data definition partitioning carefully. This is evident in the Amber Alert schema.

In that, the whole US NCIC database definition (and other US National data definitions) is separated from the rest of the GJXML definitions, making the structure of complex schemas easier to understand.

Fields Are Made Private and Set/get Properties Defined for Each Field

This is probably the first thing an enhanced XSD tool should provide. Obviously, data protection for the actual data fields is important, and by declaring the fields private and providing get/set properties, this is achieved. This also means that business rules can be coded in one place (the parent property for the object) when the field value is modified.

Un-Bounded Schema Element Sets Are Held in Typed Collections

This is then the second thing that a decent XSD extension tool should allow - a means to manage multiple element objects of the same type in a real typed collection, as opposed to the un-initialized type array generated by the XSD tool. CodeXS generates typed collections sub-classed from System.Collections.CollectionBase which support the standard operations of add, insert, and remove, as well as complete enumeration using constructs like foreach(..), and full array indexing functionality.

Default Construction of Elements if They Are Referenced but Not Yet Created

CodeXS generates automatic (default) construction of elements and attributes as they are referenced via the parent object's 'get' property and the object does not already exist. This is done wherever possible - for situations where the child element is a schema choice element, explicit construction by the application code has to be provided. A snippet from a generated code file illustrates this:

public IncidentType Incident
        // Default construction if null reference
        if ((this._incident == null))
            this._incident = new IncidentType();
        return this._incident;
        this._incident = value;

This makes the schema code class easier to use as the application code mostly does not have to construct objects as they are required.

Correct Handling of Defaulted Schema Attribute Values

The XSD tool adds the DefaultValueAttribute to a schema attribute which has a defined default value. In the generated code, the assigned value is always correct, but when the attribute is also required, the standard serialization neglects to add the attribute in the XML, seeing it as an optional attribute. This often means that serialization fails. CodeXS addresses this by removing the DefaultValueAttribute from the field (or property) definition for the attribute.

Correct Handling of Both Qualified and Unqualified Schema Element and Attribute Forms

The XSD tool does not handle element non-/-qualified forms correctly for schema elements nor schema attributes in all cases. This can lead to serialization errors, particularly for very complex schemas where both elements and attributes are often qualified. CodeXS corrects this problem and appears to work correctly in most cases.

Correct Generation of the schemaLocation or noNamespaceSchemaLocation Attribute in the Root Element

CodeXS attempts to generate these root element attributes correctly in the resultant XML output from the generated code. The code that performs this is in the Serializer class (Serializer.cs or .vb) and generates the correct URL or local disk path. This means that if you are validating XML files generated by CodeXS code classes in an XML editor such as XMLSpy, you do not have to point it to the correct location of the schema file. W3C regards these attributes as hints only: you are not required to provide the real location of the schema file. The consequence of this is that you spend a lot of time finding the schemas to validate against - a real frustration while developing.

Some Language Fix-Up Constructs to Avoid Compile Errors

Other than the 'inconsiderate' use of .NET reserved type names by some published schemas, other reserved language keywords also cause problems. This seems more prevalent for VB code generation due in part to the fact that the language definitions are not case-sensitive. There is no real way to overcome this other than scanning and changing the language definition names as required, comparing against a keyword dictionary for the language - a very ambitious task and one that CodeXS does not attempt. Instead, these are coded in an ad-hoc basis using specific ICodeModifier implementer plug-ins.

Standard Serialization Support to Easily Serialize to and From XML Strings

CodeXS provides a separate code file (Serializer.cs or .vb) that provides for common serialization support, using the System.Xml.Serialization.XmlSerializer class. It provides basic serialization support to and from XML format strings. The intent is that the Serializer class can easily be modified to provide much more functionality, or even non-XML serialization, if desired.

Extensive VS/nDoc/MSDN Compliant Documentation Comments

CodeXS automatically generates VS/nDoc compliant documentation for every class and method that is generated. The intent is that these may be modified manually after code generation. Another possibility that is currently being developed is to add the schema annotations/comments as documentation, although many published schemas do not make use of these. Currently, CodeXS does not provide additional documentation for enumerated types.

Fully Compliant with ASP.NET Web-services

XSD tool generated code classes can be used as parameter types and return value types to ASP.NET web-service web-methods. The code generated by CodeXS provides exactly the same functionality.


  • 11th September 2004: Version 0.50 ß: First release


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Willem Fourie
Web Developer
South Africa South Africa
I spend most of my time in the .NET world. I am a Systems Analyst/Consultant and Software Developer by trade. I cut my programming teeth with HPUX/C++ way back in the late '80's, but have since dabbled in most software technologies and languages since then.

I run my own company in South Africa, providing mainly software development services on a contractual basis, but I'm game for just about anything in the software development space.

I am not a Microsoft bigot, but they have to be commended for the whole .NET initiative. I think they hit a six (for the cricket players out there), or a home-run (if you prefer baseball)...

Check out my little website - there's a whole bunch of .NET goodies there...

You may also be interested in...


Comments and Discussions

QuestionNullable Data Type Pin
Hardy Wang26-Feb-10 8:07
memberHardy Wang26-Feb-10 8:07 
QuestionInclude not working Pin
Kasper G. Christensen15-Apr-08 2:49
memberKasper G. Christensen15-Apr-08 2:49 
GeneralXSDs Elements with SubstitutionGroup attribute Pin
Mr B2-Sep-07 23:52
memberMr B2-Sep-07 23:52 
Generalproblem with generated collections Pin
bergloman28-Jun-07 20:33
memberbergloman28-Jun-07 20:33 
GeneralUsing the Generated Code Pin
A Merkin20-Jun-07 0:25
memberA Merkin20-Jun-07 0:25 
GeneralRe: Using the Generated Code Pin
Willem Fourie20-Jun-07 1:58
memberWillem Fourie20-Jun-07 1:58 
GeneralRe: Using the Generated Code Pin
A Merkin20-Jun-07 2:43
memberA Merkin20-Jun-07 2:43 
GeneralRe: Using the Generated Code Pin
Willem Fourie20-Jun-07 22:49
memberWillem Fourie20-Jun-07 22:49 
GeneralRe: Using the Generated Code Pin
A Merkin21-Jun-07 5:19
memberA Merkin21-Jun-07 5:19 
GeneralA list of posts about converting XSD into a class Pin
tikotal28-Mar-07 12:53
membertikotal28-Mar-07 12:53 
GeneralRe: A list of posts about converting XSD into a class Pin
BoneSoft13-Jun-07 6:22
groupBoneSoft13-Jun-07 6:22 
GeneralFacets Pin
Y-atharth Gupta10-Jul-06 8:58
memberY-atharth Gupta10-Jul-06 8:58 
GeneralRe: Facets Pin
Willem Fourie12-Jul-06 23:10
memberWillem Fourie12-Jul-06 23:10 
QuestionKML Problem Pin
el_pablo16-Jun-06 7:53
memberel_pablo16-Jun-06 7:53 
AnswerRe: KML Problem Pin
Willem Fourie18-Jun-06 21:17
memberWillem Fourie18-Jun-06 21:17 
QuestionHow do I generate the extended features Pin
easymoney29-Apr-06 21:59
membereasymoney29-Apr-06 21:59 
AnswerRe: How do I generate the extended features Pin
Willem Fourie2-May-06 20:41
memberWillem Fourie2-May-06 20:41 
GeneralRe: How do I generate the extended features Pin
easymoney6-May-06 14:21
membereasymoney6-May-06 14:21 
QuestionTrying to understand the output. Pin
cynegetic27-Apr-06 9:00
membercynegetic27-Apr-06 9:00 
AnswerRe: Trying to understand the output. Pin
Willem Fourie27-Apr-06 22:25
memberWillem Fourie27-Apr-06 22:25 
GeneralRe: Trying to understand the output. Pin
cynegetic28-Apr-06 7:19
membercynegetic28-Apr-06 7:19 
GeneralEnabling/Disabling of "Default construction of elements" Pin
samwise_gamgee20-Apr-06 13:27
membersamwise_gamgee20-Apr-06 13:27 
GeneralRe: Enabling/Disabling of "Default construction of elements" Pin
Willem Fourie27-Apr-06 21:31
memberWillem Fourie27-Apr-06 21:31 
GeneralRe: Enabling/Disabling of "Default construction of elements" Pin
carlx28-Jan-10 0:19
membercarlx28-Jan-10 0:19 
GeneralReplacing Arrays in generated VB-Code with collections Pin
totti.w22-Mar-06 3:11
membertotti.w22-Mar-06 3:11 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190419.4 | Last Updated 29 Mar 2007
Article Copyright 2004 by Willem Fourie
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid