Comparison of XSD Code Generators
C#XsdCode GenerationC# Problem Overview
I'm doing some research in code generation from xsd schema files. My requirements:
- Must generate C# 2.0 code (or above), using generic collections where needed.
- Must generate comments from the xsd comments
- Must generate fully serializable code.
- Should be able to generate resuable basetypes when generating from multiple xsd's with the same includes.
(see also my other questions: How can I generate multiple classes from xsd’s with common includes? and How can I generate comments from xs:documentation tags in a wsdl?
I have found the following options:
- Use xsd.exe (supplied with the SDK and Visual Studio)
- XSDCodeGen from Daniel Cazzulino
- Xsd2Code
- CodeXS
- XsdObjectGen by Microsoft
- XSDClassGen (Seems to be missing in action)
Did I miss any? Because (1), (2) and (5) do not generate 2.0 code, and I have problems with serializing code from (3). What do you use when generating code?
C# Solutions
Solution 1 - C#
I believe XSD2Code is the best tool currently available (in 2011).
I recently went through the same process at work of analysing the available tools out there so i thought i would provide an updated answer that relates to VS2010.
Our main driver was that xsd.exe does not generate XML doc from the XSD annotations, which we wanted as we have hundreds of type definitions. I tried all the tools listed above as well as others and most were either deprecated, unmaintained or unable to match the current functionality of xsd.exe available in VS2010.
Xsd2Code however is a superb tool and seems to be actively maintained. It provides all the functionality that was listed above and a lot more - the CodePlex page also has great examples of how the various options affect output.
It also has tight VS integration, including context menu integration and a custom build tool (which means that if you reference the XSDs in your project and specify the custom tool, it will automatically update the code as you update the XSD). All in all saved us a lot of work.
A quick summary of the other tools i looked at:
- Dingo - Seems to be more aligned to Java
- XSDCodeGen - More of a demo on how to write a custom build tool
- CodeXS - Quite a good tool, but less integration, features and no longer maintained
- XSDObjectGen - No longer maintained, less functionality than current xsd.exe
- XSDClassGen - Could not locate it
- OXM Library - Recommend looking at this project, maintained and great functionality
- LINQ to XSD - Very cool project, but not what i was looking for
Addendum: If you do decided to go ahead with XSD2Code, there are a number of issues i found working with the command-line tool. In particular, there are some bugs with the argument processing that require some arguments to be in a certain order as well as some undocumented dependencies (eg - automatic parameters & .NET version are order specific and dependent). The following are the steps i used to generate the code using XSD2Code and then cleanup the output - take the bits that apply to you as necessary:
Run the following batch file to generate the initial code, changing the paths to the correct locations:
@echo off
set XsdPath=C:\schemas
set OutPath=%XsdPath%\Code
set ExePath=C:\Progra~1\Xsd2Code
set Namespace=InsertNamespaceHere
echo.Starting processing XSD files ...
for /f %%a IN ('dir %XsdPath%\*.xsd /a-d /b /s') do call:ProcessXsd %%a
echo.Finished processing XSD files ...
echo.&pause&
goto:eof
:ProcessXsd
%ExePath%\Xsd2Code %~1 %Namespace% %XsdPath%\Code\%~n1%.cs /pl Net35 /if- /dc /sc /eit
echo.Processed %~n1
goto:eof
Perform the following steps to tidy up the generated code, as necessary:
- Regex replace - current project, case, whole word - [System.Runtime.Serialization.DataContractAttribute(Name:b=:b:q,:bNamespace:b=:b*{:q})] with [DataContract(Namespace = \1)]**
- Replace - current project, case, whole word - [System.Runtime.Serialization.DataMemberAttribute()] with [DataMember]
- Regex replace - current project, case, whole word - System.Nullable<{:w}> with \1?
- Regex replace - open documents, case, whole word - {:w}TYPE with \1
- Replace - open documents, case, whole word - System.DateTime with DateTime, then add missing using statements
- Replace - open documents, case, whole word - [System.Xml.Serialization.XmlIgnoreAttribute()] with [XmlIgnore]
- Replace - current project - System.Xml.Serialization.XmlArrayAttribute with XmlArray
- Replace - current project - System.Xml.Serialization.XmlArrayItemAttribute with XmlArrayItem
- Regex replace - current project - ,[:Wh]+/// <remarks/> with ,
Hope that helps someone.
Solution 2 - C#
I have not yet checked this out, but Linq2XSD might be a useful alternative.
I'm going to give this one a shot. LINQ with XSD generation would be better than any of these tools you mentioned - provided it works nicely.
Solution 3 - C#
OpenSource project XSD to Classes worked perfect for me.
Solution 4 - C#
I a project a bit over a year ago we used CodeXS. With some minor adjustments (a script that cleaned up the generated code a bit) it worked a charm.
There is also Dingo, which have some very good extensibility features (which we didn't need).
Solution 5 - C#
Try OxmLibrary - [http://oxmlibrary.codeplex.com][1]
[1]: http://oxmlibrary.codeplex.com "This Link"
Solution 6 - C#
The best XSD class generator I've found is thinktecture WSCF.blue . It's nicer than most of the others for two reasons:
- Fixes naming. That means casing and plurization of types and property names.
- Creates a separate file for each class.
Or, if you're looking for a T4 solution, you can try XsdClassGen . This one isn't working for me. But the good news is that it's a T4 file, so it's easy to fix!
Solution 7 - C#
Here is web based example of using XSLT to transform XML to C# code. The example takes a model (XML) that describes services (basically service names, namespaces, and list of operations), it then generates WCF services (interfaces, messages, faults, tests, etc) all in C#.net.