本文翻译自:How to generate .NET 4.0 classes from xsd?
使用Visual Studio 2010从xsd文件生成.NET 4.0 c#类(实体)有哪些选项?
#1楼
参考:https://stackoom.com/question/lTlT/如何从xsd生成-NET-类
#2楼
xsd.exe does not work well when you have circular references (ie a type can own an element of its own type directly or indirectly). 当你有循环引用时,xsd.exe不能正常工作(即一个类型可以直接或间接拥有自己类型的元素)。
When circular references exist, I use Xsd2Code. 当存在循环引用时,我使用Xsd2Code。 Xsd2Code handles circular references well and works within the VS IDE, which is a big plus. Xsd2Code很好地处理循环引用,并在VS IDE中工作,这是一个很大的优点。 It also has a lot of features you can use like generating the serialization/deserialization code. 它还具有许多功能,例如生成序列化/反序列化代码。 Make sure you turn on the GenerateXMLAttributes if you are generating serialization though (otherwise you'll get exceptions for ordering if not defined on all elements). 如果您正在生成序列化,请确保打开GenerateXMLAttributes(否则如果未在所有元素上定义,您将获得排序异常)。
Neither works well with the choice feature. 两者都不适用于选择功能。 you'll end up with lists/collections of object instead of the type you want. 你最终会得到对象的列表/集合而不是你想要的类型。 I'd recommend avoiding choice in your xsd if possible as this does not serialize/deserialize well into a strongly typed class. 如果可能的话,我建议在xsd中避免选择,因为这不会很好地序列化/反序列化为强类型类。 If you don't care about this, though, then it's not a problem. 如果你不关心这个,那么这不是问题。
The any feature in xsd2code deserializes as System.Xml.XmlElement which I find really convenient but may be an issue if you want strong typed objects. xsd2code中的任何功能反序列化为System.Xml.XmlElement,我觉得这很方便但如果你想要强类型对象可能会有问题。 I often use any when allowing custom config data, so an XmlElement is convenient to pass to another XML deserializer that is custom defined elsewhere. 我经常在允许自定义配置数据时使用any,因此XmlElement可以方便地传递给另一个在其他地方自定义的XML反序列化器。
#3楼
I use XSD
in a batch script to generate .xsd
file and classes from XML
directly : 我在批处理脚本中使用XSD
直接从XML
生成.xsd
文件和类:
set XmlFilename=Your__Xml__Here
set WorkingFolder=Your__Xml__Path_Here
set XmlExtension=.xml
set XsdExtension=.xsd
set XSD="C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1\Tools\xsd.exe"
set XmlFilePath=%WorkingFolder%%XmlFilename%%XmlExtension%
set XsdFilePath=%WorkingFolder%%XmlFilename%%XsdExtension%
%XSD% %XmlFilePath% /out:%WorkingFolder%
%XSD% %XsdFilePath% /c /out:%WorkingFolder%
#4楼
Marc Gravells answer was right for me but my xsd was with extension of .xml. Marc Gravells的回答对我来说是对的,但是我的xsd扩展名为.xml。 When I used xsd program it gave : 当我使用xsd程序时,它给出了: - The table (Amt) cannot be the child table to itself in nested relations.
As per this KB325695 I renamed extension from .xml to .xsd and it worked fine. 根据这个KB325695,我将扩展名从.xml重命名为.xsd,并且工作正常。
#5楼
I used xsd.exe
in the Windows command prompt. 我在Windows命令提示符下使用了xsd.exe
。
However, since my xml referenced several online xml's (in my case http://www.w3.org/1999/xlink.xsd
which references http://www.w3.org/2001/xml.xsd
) I had to also download those schematics, put them in the same directory as my xsd, and then list those files in the command: 但是,由于我的xml引用了几个在线xml(在我的案例中http://www.w3.org/1999/xlink.xsd
引用了http://www.w3.org/2001/xml.xsd
)我还必须下载这些原理图,将它们放在与我的xsd相同的目录中,然后在命令中列出这些文件:
"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools\\xsd.exe" /classes /language:CS your.xsd xlink.xsd xml.xsd “C:\\ Program Files(x86)\\ Microsoft SDKs \\ Windows \\ v8.1A \\ bin \\ NETFX 4.5.1 Tools \\ xsd.exe”/ classes / language:CS your.xsd xlink.xsd xml.xsd
#6楼
simple enough; 很简单; just run (at the vs command prompt) 刚运行(在vs命令提示符下)
xsd your.xsd /classes
(which will create your.cs
). (这将创建your.cs
)。 Note, however, that most of the intrinsic options here haven't changed much since 2.0 但请注意,自2.0以来,此处的大多数内在选项都没有太大变化
For the options, use xsd /?
对于选项,使用xsd /?
or see MSDN ; 或者看MSDN ; for example /enableDataBinding
can be useful. 例如/enableDataBinding
可能很有用。