我正在研究从 xsd 模式文件生成代码.我的要求:
I'm doing some research in code generation from xsd schema files. My requirements:
(另请参阅我的其他问题:如何从具有常见包含的 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)、(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:
附录:如果您决定继续使用 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:
希望对某人有所帮助.
这篇关于XSD 代码生成器的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!