問題描述
我正在研究從 xsd 模式文件生成代碼.我的要求:
I'm doing some research in code generation from xsd schema files. My requirements:
- 必須生成 C# 2.0(或更高版本)代碼,并在需要時使用泛型集合.
- 必須從 xsd 評論中生成評論
- 必須生成完全可序列化的代碼.
- 從具有相同包含的多個 xsd 生成時,應(yīng)該能夠生成可重復(fù)使用的基本類型.
(另請參閱我的其他問題:如何從具有常見包含的 xsd 生成多個類? 和 如何從 wsdl 中的 xs:documentation 標(biāo)簽生成注釋?
(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.exe(隨 SDK 和 Visual Studio 提供)
- XSDCodeGen 來自 Daniel Cazzulino 李>
- Xsd2Code
- CodeXS
- XsdObjectGen 由微軟提供
- XSDClassGen(似乎在行動中缺失)
- Use xsd.exe (supplied with the SDK and Visual Studio)
- XSDCodeGen from Daniel Cazzulino
- Xsd2Code
- CodeXS
- XsdObjectGen by Microsoft
- 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).
我最近在分析可用工具時經(jīng)歷了相同的過程,所以我想我會提供與 VS2010 相關(guān)的更新答案.
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.
我們的主要驅(qū)動因素是 xsd.exe 不會從 XSD 注釋生成 XML 文檔,這是我們想要的,因為我們有數(shù)百個類型定義.我嘗試了上面列出的所有工具以及其他工具,但大多數(shù)工具要么已棄用、未維護或無法匹配 VS2010 中可用的 xsd.exe 的當(dāng)前功能.
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 集成,包括上下文菜單集成和自定義構(gòu)建工具(這意味著如果您在項目中引用 XSD 并指定自定義工具,它將在您更新 XSD 時自動更新代碼).總而言之,為我們節(jié)省了很多工作.
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.
我看過的其他工具的快速總結(jié):
A quick summary of the other tools i looked at:
- Dingo - 似乎更符合 Java
- XSDCodeGen - 更多關(guān)于如何編寫自定義構(gòu)建工具的演示
- CodeXS - 相當(dāng)不錯的工具,但集成度低、功能少且不再維護
- XSDObjectGen - 不再維護,功能比當(dāng)前 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
附錄:如果您決定繼續(xù)使用 XSD2Code,那么我發(fā)現(xiàn)使用命令行工具會遇到許多問題.特別是,參數(shù)處理存在一些錯誤,這些錯誤需要一些參數(shù)按特定順序以及一些未記錄的依賴項(例如 - 自動參數(shù)和 .NET 版本是特定于順序和依賴的).以下是我使用 XSD2Code 生成代碼然后清理輸出的步驟 - 根據(jù)需要獲取適用于您的位:
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
根據(jù)需要執(zhí)行以下步驟來整理生成的代碼:
Perform the following steps to tidy up the generated code, as necessary:
- 正則表達式替換 - 當(dāng)前項目、大小寫、整個單詞 - [System.Runtime.Serialization.DataContractAttribute(Name:b*=:b*:q,:bNamespace:b=:b*{:q})] 與 [DataContract(Namespace = 1)]
- 用 [DataMember] 替換 - 當(dāng)前項目、案例、整個單詞 - [System.Runtime.Serialization.DataMemberAttribute()]
- 正則表達式替換 - 當(dāng)前項目、大小寫、整個單詞 - System.Nullable<{:w}> 與 1?
- 正則表達式替換 - 打開文檔、大小寫、整個單詞 - {:w}TYPE 與 1
- 用 DateTime 替換 - 打開文檔、大小寫、整個單詞 - System.DateTime,然后添加缺少的 using 語句
- 替換 - 打開文檔、大小寫、整個單詞 - [System.Xml.Serialization.XmlIgnoreAttribute()] 為 [XmlIgnore]
- 替換 - 當(dāng)前項目 - System.Xml.Serialization.XmlArrayAttribute 為 XmlArray
- 替換 - 當(dāng)前項目 - System.Xml.Serialization.XmlArrayItemAttribute 為 XmlArrayItem
- 正則表達式替換 - 當(dāng)前項目 - ,[:Wh]+///<remarks/> 與 ,
- Regex replace - current project, case, whole word - [System.Runtime.Serialization.DataContractAttribute(Name:b*=:b*:q,:bNamespace:b=:b*{:q})] with [DataContract(Namespace = 1)]
- Replace - current project, case, whole word - [System.Runtime.Serialization.DataMemberAttribute()] with [DataMember]
- Regex replace - current project, case, whole word - System.Nullable<{:w}> with 1?
- Regex replace - open documents, case, whole word - {:w}TYPE with 1
- Replace - open documents, case, whole word - System.DateTime with DateTime, then add missing using statements
- Replace - open documents, case, whole word - [System.Xml.Serialization.XmlIgnoreAttribute()] with [XmlIgnore]
- Replace - current project - System.Xml.Serialization.XmlArrayAttribute with XmlArray
- Replace - current project - System.Xml.Serialization.XmlArrayItemAttribute with XmlArrayItem
- Regex replace - current project - ,[:Wh]+/// <remarks/> with ,
希望對某人有所幫助.
這篇關(guān)于XSD 代碼生成器的比較的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!