XSD 代码生成器的比较

时间:2023-03-30
本文介绍了XSD 代码生成器的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

  • 必须生成 C# 2.0(或更高版本)代码,并在需要时使用泛型集合.
  • 必须从 xsd 评论中生成评论
  • 必须生成完全可序列化的代码.
  • 从具有相同包含的多个 xsd 生成时,应该能够生成可重复使用的基本类型.

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

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

我找到了以下选项:

  1. 使用 xsd.exe(随 SDK 和 Visual Studio 提供)
  2. XSDCodeGen 来自 Daniel Cazzulino 李>
  3. Xsd2Code
  4. CodeXS
  5. XsdObjectGen 由微软提供
  6. XSDClassGen(似乎在行动中缺失)
  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)

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

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?

推荐答案

我相信 XSD2Code 是最好的工具目前可用(2011 年).

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

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

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.

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

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

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.

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

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 - 似乎更符合 Java
  • XSDCodeGen - 更多关于如何编写自定义构建工具的演示
  • CodeXS - 相当不错的工具,但集成度低、功能少且不再维护
  • XSDObjectGen - 不再维护,功能比当前 xsd.exe 少
  • XSDClassGen - 找不到它
  • OXM 库 - 推荐看看这个项目,维护和强大的功能
  • LINQ to XSD - 非常项目,但不是我想要的对于
  • 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

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

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~1Xsd2Code
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. 正则表达式替换 - 当前项目、大小写、整个单词 - [System.Runtime.Serialization.DataContractAttribute(Name:b*=:b*:q,:bNamespace:b=:b*{:q})] 与 [DataContract(Namespace = 1)]
  2. [DataMember]
  3. 替换 - 当前项目、案例、整个单词 - [System.Runtime.Serialization.DataMemberAttribute()]
  4. 正则表达式替换 - 当前项目、大小写、整个单词 - System.Nullable<{:w}>1?
  5. 正则表达式替换 - 打开文档、大小写、整个单词 - {:w}TYPE1
  6. DateTime 替换 - 打开文档、大小写、整个单词 - System.DateTime,然后添加缺少的 using 语句
  7. 替换 - 打开文档、大小写、整个单词 - [System.Xml.Serialization.XmlIgnoreAttribute()][XmlIgnore]
  8. 替换 - 当前项目 - System.Xml.Serialization.XmlArrayAttributeXmlArray
  9. 替换 - 当前项目 - System.Xml.Serialization.XmlArrayItemAttributeXmlArrayItem
  10. 正则表达式替换 - 当前项目 - ,[:Wh]+///<remarks/>,
  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 ,

希望对某人有所帮助.

这篇关于XSD 代码生成器的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何从 xsd 生成 .NET 4.0 类? 下一篇:XSDObjectGen.exe 与 XSD.exe

相关文章