76

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:

  1. Use xsd.exe (supplied with the SDK and Visual Studio)
  2. XSDCodeGen from Daniel Cazzulino
  3. Xsd2Code
  4. CodeXS
  5. XsdObjectGen by Microsoft
  6. 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?

Community
  • 1
  • 1
edosoft
  • 17,121
  • 25
  • 77
  • 111
  • What kind of problems have you had with Xsd2Code? I haven't had any problems with it. – John Kraft Sep 02 '09 at 03:09
  • on 5) http://stackoverflow.com/questions/607085/looking-for-a-download-location-for-xsdclassgen/607146#607146 has 2.0 XSDObjectGen link to source – Greg Domjan Dec 30 '09 at 21:56

7 Answers7

62

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:

  1. Regex replace - current project, case, whole word - [System.Runtime.Serialization.DataContractAttribute(Name:b=:b:q,:bNamespace:b=:b*{:q})] with [DataContract(Namespace = \1)]**
  2. Replace - current project, case, whole word - [System.Runtime.Serialization.DataMemberAttribute()] with [DataMember]
  3. Regex replace - current project, case, whole word - System.Nullable<{:w}> with \1?
  4. Regex replace - open documents, case, whole word - {:w}TYPE with \1
  5. Replace - open documents, case, whole word - System.DateTime with DateTime, then add missing using statements
  6. Replace - open documents, case, whole word - [System.Xml.Serialization.XmlIgnoreAttribute()] with [XmlIgnore]
  7. Replace - current project - System.Xml.Serialization.XmlArrayAttribute with XmlArray
  8. Replace - current project - System.Xml.Serialization.XmlArrayItemAttribute with XmlArrayItem
  9. Regex replace - current project - ,[:Wh]+/// &lt;remarks/&gt; with ,
starball
  • 20,030
  • 7
  • 43
  • 238
Xcalibur
  • 3,613
  • 2
  • 32
  • 26
  • 2
    I have to agree that Xsd2Code is currently the champ. Just watch out for the add-in not loading correctly if you have moved your default 'Documents' location, either manually or via GPO. Solution is to grab the add-in file from the install location and place it in the Documents\Visual Studio 2010\Addins\ folder. – Stephen Patten Apr 27 '11 at 23:21
  • 1
    xsd2code seems dead. I hope a new version is forthcoming, there is a major blocking bug and a couple of other bugs that prevent us from using this realistically. – womp Jul 15 '11 at 16:15
  • Just a note: I came across XsdClassGen on Nuget (see http://nuget.org/List/Packages/XsdClassGen). It uses a T4 template, which is nice, but it's very primitive. – devuxer Oct 28 '11 at 22:37
  • 2
    Added by `Schalk Versteeg` as edit: `XSDtoClasses` https://code.google.com/p/xsd-to-classes/ - `Extendable VS plugin, very similar in capabilities to XSD2Code.` – Rookie Programmer Aravind Mar 12 '13 at 09:08
  • 3
    XSD2Code doesn't work for Visual Studio 2012. – cederlof Oct 15 '13 at 15:31
  • The data contract param `/dc` wasn't working for me until I used exactly the combination and order of params in this post: `/pl Net35 /if- /dc /sc` – AaronLS Apr 01 '14 at 18:11
  • I would add that it does not properly handle attributes, and instead handles them the same as elements. There are workaround for data contracts to implement support for xml attributes that you will have to handle manually: http://stackoverflow.com/questions/4858798/datacontract-xml-serialization-and-xml-attributes – AaronLS Apr 01 '14 at 20:16
  • Nov 2015 - The xsd2code site is still up, but no trial licenses are being issued and email to the site is bouncing. No updates since January. I think this vendor has abandoned the product. – Clint StLaurent Nov 05 '15 at 14:56
  • The addin won't work in VS2015 (because it's an addin), but the library DLL is still compatible. – Grault Nov 11 '15 at 23:03
  • For your information : https://marketplace.visualstudio.com/search?term=xsd2code&target=VS&category=All%20categories&vsVersion=&sortBy=Relevance The 5 stars Xsd2Code 2015 and 2017 integration has all the options we need with the dialog and generate our xml very nicely. – Marc Roussel Sep 13 '17 at 09:56
  • Xsd2Code is no longer free – Mirek May 16 '18 at 11:11
9

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.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 5
    don't be too concerned its a 'dead project'. if its pretty much complete and working then its not 'dead' - its working! seems ok for me so far – Simon_Weaver Jul 14 '09 at 01:47
  • 2
    I'm currently successfully using Linq to Xsd in an active project. It seems to be the most 'complete' xsd to class mapper that I've found. I tried using Xsd2Code, but it didn't map the complex types of the xsd very well - I had a xs:pattern restriction that is mapped perfectly in Linq to Xsd, but is missing in Xsd2Code. Top tip - download the source for Linq to Xsd - it has a whole bunch of docs and example projects that aren't listed in the 'documents' tab of codeplex - [download here](http://linqtoxsd.codeplex.com/SourceControl/list/changesets) – Lanceomagnifico Mar 02 '11 at 09:22
  • 3
    I've been using it a couple years now. It has some huge advantages over other entity technologies, and I think it's a real shame MS abandoned it – Simon_Weaver Mar 03 '11 at 01:05
  • Doesn't work with Visual Studio 2012. – cederlof Oct 15 '13 at 15:29
  • Still using it! (Not for new stuff, but it still works under .NET Core). – Simon_Weaver Jul 24 '20 at 11:01
3

OpenSource project XSD to Classes worked perfect for me.

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
2

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

Steen
  • 6,573
  • 3
  • 39
  • 56
2

Try OxmLibrary - http://oxmlibrary.codeplex.com

maor
  • 86
  • 1
0

The best XSD class generator I've found is thinktecture WSCF.blue . It's nicer than most of the others for two reasons:

  1. Fixes naming. That means casing and plurization of types and property names.
  2. 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!

Josh Mouch
  • 3,480
  • 1
  • 37
  • 34
-2

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.

Paul Fryer
  • 9,268
  • 14
  • 61
  • 93