C# XSD 代码生成器的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/386155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:28:06  来源:igfitidea点击:

Comparison of XSD Code Generators

c#xsdcode-generation

提问by edosoft

I'm doing some research in code generation from xsd schema files. My requirements:

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

  • 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.
  • 必须生成 C# 2.0 代码(或更高版本),并在需要时使用泛型集合。
  • 必须从 xsd 注释生成注释
  • 必须生成完全可序列化的代码。
  • 从具有相同包含的多个 xsd 生成时,应该能够生成可重用的基类型。

(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?

(另请参阅我的其他问题:如何从具有公共包含的 xsd 生成多个类?以及如何从 wsdl 中的 xs:documentation 标签生成注释?

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. XsdObjectGenby Microsoft
  6. XSDClassGen(Seems to be missing in action)
  1. 使用 xsd.exe(随 SDK 和 Visual Studio 提供)
  2. 来自Daniel Cazzulino 的XSDCodeGen
  3. Xsd2代码
  4. 代码XS
  5. 微软的XsdObjectGen
  6. XSDClassGen(似乎在行动中缺失)

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?

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

采纳答案by Xcalibur

I believe XSD2Codeis the best tool currently available (in 2011).

我相信XSD2Code是目前可用的最佳工具(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.

我最近在分析可用工具时经历了相同的过程,所以我想我会提供与VS2010相关的更新答案。

Our main driver was that xsd.exedoes 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.

我们的主要驱动力是xsd.exe不会从 XSD 注释生成 XML 文档,这是我们想要的,因为我们有数百个类型定义。我尝试了上面列出的所有工具以及其他工具,但大多数都已弃用、未维护或无法匹配 VS2010 中可用的 xsd.exe 的当前功能。

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

然而,Xsd2Code是一个极好的工具,并且似乎得到了积极维护。它提供了上面列出的所有功能以及更多功能 - CodePlex 页面还提供了各种选项如何影响输出的很好的示例。

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.

它还具有紧密的 VS 集成,包括上下文菜单集成和自定义构建工具(这意味着如果您在项目中引用 XSD 并指定自定义工具,它会在您更新 XSD 时自动更新代码)。总而言之,为我们节省了很多工作。

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- Recommendlooking at this project, maintained and great functionality
  • LINQ to XSD- Very coolproject, but not what i was looking for
  • Dingo- 似乎更符合 Java
  • XSDCodeGen - 更多关于如何编写自定义构建工具的演示
  • CodeXS- 相当不错的工具,但集成度、功能和不再维护
  • XSDObjectGen - 不再维护,功能比当前的 xsd.exe 少
  • XSDClassGen - 找不到它
  • OXM 库-推荐查看这个项目,维护和强大的功能
  • LINQ to XSD- 非常酷的项目,但不是我想要的

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:

附录:如果您确实决定继续使用 XSD2Code,那么我在使用命令行工具时发现了许多问题。特别是,参数处理存在一些错误,需要一些参数按特定顺序以及一些未记录的依赖项(例如 - 自动参数和 .NET 版本是特定于顺序和依赖的)。以下是我使用 XSD2Code 生成代码然后清理输出的步骤 - 根据需要获取适用于您的位:

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}TYPEwith \1
  5. Replace - open documents, case, whole word - System.DateTimewith 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.XmlArrayAttributewith XmlArray
  8. Replace - current project - System.Xml.Serialization.XmlArrayItemAttributewith XmlArrayItem
  9. Regex replace - current project - ,[:Wh]+/// \<remarks/\>with ,
  1. 正则表达式替换 - 当前项目,案例,整个词 - [System.Runtime.Serialization.DataContractAttribute(Name:b*=:b*:q,:b Namespace:b=:b*{:q})] 与 [DataContract(命名空间 = \1)]
  2. [DataMember]替换 - 当前项目、案例、全字 - [System.Runtime.Serialization.DataMemberAttribute() ]
  3. 正则表达式替换 - 当前项目、案例、整个单词 - System.Nullable\<{:w}>\1\?
  4. 正则表达式替换 - 打开文档,大小写,整个单词 - {:w}TYPEwith \1
  5. 替换 - 打开文档、案例、整个单词 - System.DateTimeDateTime,然后添加缺少的 using 语句
  6. 替换 - 打开文档、案例、整个单词 - [System.Xml.Serialization.XmlIgnoreAttribute()][XmlIgnore]
  7. XmlArray替换 - 当前项目 - System.Xml.Serialization.XmlArrayAttribute
  8. XmlArrayItem替换 - 当前项目 - System.Xml.Serialization.XmlArrayItemAttribute
  9. 正则表达式替换 - 当前项目 - ,[:Wh]+/// \<remarks//>,

Hope that helps someone.

希望能帮助某人。

回答by Steen

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.

一年多以前,我的一个项目使用了 CodeXS。通过一些小的调整(一个稍微清理生成代码的脚本),它很有魅力。

There is also Dingo, which have some very good extensibility features (which we didn't need).

还有Dingo,它有一些非常好的扩展特性(我们不需要)。

回答by Simon_Weaver

I have not yet checked this out, but Linq2XSDmight be a useful alternative.

我还没有检查过这个,但Linq2XSD可能是一个有用的替代方案。

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.

我要试一试。具有 XSD 生成功能的 LINQ 比您提到的任何这些工具都要好 - 只要它运行良好。

回答by maor

回答by Stef Heyenrath

OpenSource project XSD to Classesworked perfect for me.

开源项目XSD to Classes非常适合我。

回答by Josh Mouch

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

我发现的最好的 XSD 类生成器是 thinktecture WSCF.blue。它比大多数其他人更好,原因有两个:

  1. Fixes naming. That means casing and plurization of types and property names.
  2. Creates a separate file for each class.
  1. 修复命名。这意味着类型和属性名称的大小写和多元化。
  2. 为每个类创建一个单独的文件。

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!

或者,如果您正在寻找 T4 解决方案,您可以尝试 XsdClassGen。这个对我不起作用。但好消息是它是一个 T4 文件,所以很容易修复!

回答by Paul Fryer

Here is web based exampleof 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.

这是使用 XSLT 将 XML 转换为 C# 代码的基于 Web 的示例。该示例采用描述服务(基本上是服务名称、命名空间和操作列表)的模型 (XML),然后在 C#.net 中生成 WCF 服务(接口、消息、故障、测试等)。