Saturday 14 November 2020

C# Code Generator from Schema

When you work in the back end, once in a while, you will need to generate code from XML schema. Many applications define their data model using XML schema. Particularly when working with NHS projects and working with the integration with legacy NHS services, which is based on HL7 V3 MIM (Manual Implementation manuals) or DMS (Domain Message Specifications). I need a library in C# to work with the data models. In the past, when I was still working for a primary NHS GP provider, we used a modified version of CodeXS, which is not available anymore publicly. 

In my current company, a private GP provider, I need to develop the tooling myself. After researching for a while, I chose xsd2code for the tooling. However, there are a couple of tweaks required to make it works with MIM and DMS schemas. I implemented them as switches in the command line arguments. Though xsd2code is popular with its integration with Visual Studio IDE, for the MIM and DMS data models we need it as a command-line tool. 

The source code Xsd2code is available publicly until Visual Studio 2015, and since then it became a commercial product. The subsequent source code development is not available publicly anymore. The old source code is available as an archived project in codeplex here, and have an MIT license.

The source code is copied to GitHub with a couple of tweaks by adding additional switches to the xsd2code command line, and provided with the original MIT license.

/mOutput multiple files from a schema. A schema may generate a number of classes and by default, it outputs them in a single file. This switch outputs them each of the class in its own files. This is particularly useful for MIM and DMS data models, as most schemas will share a large number of dependant classes, and this allows them to share these classes. 
/sdefault 
or
/sdefault+
 serializing default value attribute
/sdefault-not serializing default value attribute
/sskipnull 
or 
/sskipnull+
skip nullable element when serializing
/sskipnull-serialize nullable element as an element with an attribute xsi|:null=true

To use the code generator, go to this github link and download the release bundle on the right panel. 
there are two files:
* xsd2code_x64.exe (as 64 bit application)
* xsd2code_x86.exe (as 32 bit application)

You can install either of them. Once you have installed the application, add the installation folder in your windows environment PATH variable. 

An example of usage: 

xsd2Code [schemafile]  [namespace of the generated classes]   [output folder]   /m /pl Net35 /if- /xa /sc /is /cl /c Array /eit /sdefault+ /sskipnull+

To view the meaning of the other switches, just run xsd2code.
The modified source code can be downloaded from here 

Their usage in the NHS MIM and DMS class library will be discussed in other posts. 

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete