XSD 代码生成器的比较

我正在研究从 xsd 模式文件生成代码。 我的要求:

  • 必须生成 C # 2.0代码(或更高版本) ,在需要时使用通用集合。
  • 必须从 xsd 注释生成注释
  • 必须生成完全可序列化的代码。
  • 当使用相同的 include 从多个 xsd 生成时,应该能够生成可重用的基类。

(另见我的其他问题: 如何使用通用包含从 xsd 生成多个类?如何从 wsdl 中的 xs: document 标记生成注释?

我发现了以下选择:

  1. 使用 xsd.exe (与 SDK 和 VisualStudio 一起提供)
  2. 来自 Daniel Cazzulino的 XSDCodeGen
  3. Xsd2Code
  4. CodeXS
  5. 微软的 XsdObjectGen
  6. XSDClassGen (似乎在操作中丢失了)

我错过什么了吗?因为(1)、(2)和(5)不生成2.0代码,而且我在序列化(3)中的代码时遇到了问题。生成代码时使用什么?

58536 次浏览

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

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.

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]+/// \<remarks/\> with ,

Hope that helps someone.

OpenSource project XSD to Classes worked perfect for me.

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!

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.