Generating C# code files for B2MML schema’s (2)
I tried generating C# code files from the B2MML-V0401 xsd schema’s using xsd.exe. It required utilizing a bug in xsd.exe, typing in a command with as many parameters as xsd schema’s and resulted in a single, huge code file with code that I didn’t really like. It worked, but I wasn’t completely satisfied (see my previous post on this subject). After posting my findings to the WBF XML newsgroup, I got a useful reply from Nick.
Nick advised me to create a single schema file that includes the B2MML-V0401 xsd schema’s for which code should be generated and name it (for instance) B2MML-V0401.xsd. Then run xsd B2MML-V0401-AllExtensions.xsd .\B2MML-V0401.xsd /c on this file and voil?, you’ll get the code generated into B2MML-V0401.cs.
I like this method, because the command is readable and I can cleanly specify the schema’s to use for input.
However, it still left me with the code I didn’t really like. I’d like:
- generic lists instead of arrays
- the ability to generate data contract attributes
- to be able to automatically put the code in separate code files
Nick pointed out that his company provides proprietary libraries that are compatible with B2MML messages and that provides the API’s I prefer. At this time however, I don’t want to go the proprietary road. However, if I can get my hands on an evaluation version, I’ll experiment a bit with it.
I spend some, or rather too much, time searching for a freely available tool to do this code generation for me. I came across CodeXS en XsdObjectGen, but I found it cumbersome to start using them, and their current status was unclear to me. I ended up using Xsd2Code, which enables me to generate the code I like from within Visual Studio (2008). It also has a command line tool, but I haven’t checked it out yet.
Any suggestions on code generation tools (preferably open source) are welcome.
Generating C# code files for B2MML schemas
Recently I was doing some proof-of-concepts for using B2MML messages in my .NET/C# application. I wanted to be able to generate xml-serializable C# classes, compliant to the B2MML xsd schema’s. Since the xsd schema’s are available on the WBF website, I figured I’d simply run those through xsd.exe and start coding away.
However, for some mysterious reason, you are not able to specify the filename of the .cs output file xsd.exe generates. By default it concatenates the names of all the schema’s you pass into it. If you generate a code file for the twenty-or-so schema’s in the B2MML implementation, xsd.exe chooses a filename that is awkward to read and, worse, too large to be handled by my Windows installation.
I found a work-around for this issue on stackoverflow. It appears that there is a known bug in xsd.exe that you can exploit to get a short, readable filename: pass the last schema using the “.” path characters. Now xsd.exe only uses the last schema in the output filename. So I created an empty xsd schema named B2MML-V0401.xsd and ran
xsd B2MML-V0401-AllExtensions.xsd B2MML-V0401-Common.xsd [other schemas go here] .\B2MML-V0401.xsd /c
Which resulted in the file B2MML-V0401.cs.
The resulting C# code is usable in my application, however there are some things that still bother me:
- I don’t really like the code that is generated (amongst other things I’d prefer generic lists over arrays and all code ends up in one huge 30k+ LOC codefile)
- I don’t like the fact that I utilize a known bug in xsd.exe
- The command needs many parameters, which makes it unreadable.
I posted my work-around to the WBF XML newsgroup and got some useful replies to overcome these issues. I’ll investigate those and post my findings here.