如何从xsd生成。net 4.0类?

时间:2022-12-30 17:19:31

What are the options to generate .NET 4.0 c# classes (entities) from an xsd file, using Visual Studio 2010?

使用Visual Studio 2010从xsd文件生成。net 4.0 c#类(实体)的选项有哪些?

6 个解决方案

#1


382  

simple enough; just run (at the vs command prompt)

很简单;只需运行(在vs命令提示符下)

xsd your.xsd /classes

(which will create your.cs). Note, however, that most of the intrinsic options here haven't changed much since 2.0

(这将创建your.cs)。但是请注意,这里的大多数固有选项自2.0以来并没有太大的变化。

For the options, use xsd /? or see MSDN; for example /enableDataBinding can be useful.

对于选项,使用xsd /?或查看MSDN;例如/enableDataBinding可能有用。

#2


51  

xsd.exe as mentioned by Marc Gravell. The fastest way to get up and running IMO.

xsd。正如Marc Gravell所提到的。最快的方式起床和运行的国际海事组织。

Or if you need more flexibility/options :

或者如果你需要更多的灵活性/选择:

xsd2code VS add-in (Codeplex)

xsd2code VS插件(Codeplex上)

#3


13  

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 handles circular references well and works within the VS IDE, which is a big plus. 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).

当循环引用存在时,我使用Xsd2Code。Xsd2Code能够很好地处理循环引用,并且在VS IDE中工作,这是一个很大的优点。它还具有许多特性,比如生成序列化/反序列化代码。如果您正在生成序列化,请确保您打开了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. If you don't care about this, though, then it's not a problem.

这两款产品都不支持选择功能。最终您将得到对象的列表/集合,而不是您想要的类型。如果可能,我建议避免在xsd中进行选择,因为这不会很好地将/反序列化到强类型类中。如果你不关心这个,那就没问题了。

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

xsd2code中的any特性将反序列化为System.Xml。XmlElement,我觉得它非常方便,但如果你想要强类型对象,它可能是个问题。当允许自定义配置数据时,我经常使用any,因此XmlElement可以方便地传递到其他地方自定义的另一个XML反序列化器。

#4


1  

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%

#5


1  

Marc Gravells answer was right for me but my xsd was with extension of .xml. When I used xsd program it gave :
- The table (Amt) cannot be the child table to itself in nested relations.

Marc Gravells的回答对我来说是正确的,但我的xsd使用了.xml扩展名。当我使用xsd程序时,它给出了:-表(Amt)不能是嵌套关系中的子表。

As per this KB325695 I renamed extension from .xml to .xsd and it worked fine.

根据KB325695,我将扩展名从.xml重命名为.xsd,效果很好。

#6


0  

I used xsd.exe in the Windows command prompt.

我使用了xsd。在Windows命令提示符中的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

\程序文件(x86)\微软SDKs\Windows\v8.1A\bin\NETFX 4.5.1工具\xsd。exe”/类/语言:c。xsd xlink。xsd xml.xsd

#1


382  

simple enough; just run (at the vs command prompt)

很简单;只需运行(在vs命令提示符下)

xsd your.xsd /classes

(which will create your.cs). Note, however, that most of the intrinsic options here haven't changed much since 2.0

(这将创建your.cs)。但是请注意,这里的大多数固有选项自2.0以来并没有太大的变化。

For the options, use xsd /? or see MSDN; for example /enableDataBinding can be useful.

对于选项,使用xsd /?或查看MSDN;例如/enableDataBinding可能有用。

#2


51  

xsd.exe as mentioned by Marc Gravell. The fastest way to get up and running IMO.

xsd。正如Marc Gravell所提到的。最快的方式起床和运行的国际海事组织。

Or if you need more flexibility/options :

或者如果你需要更多的灵活性/选择:

xsd2code VS add-in (Codeplex)

xsd2code VS插件(Codeplex上)

#3


13  

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 handles circular references well and works within the VS IDE, which is a big plus. 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).

当循环引用存在时,我使用Xsd2Code。Xsd2Code能够很好地处理循环引用,并且在VS IDE中工作,这是一个很大的优点。它还具有许多特性,比如生成序列化/反序列化代码。如果您正在生成序列化,请确保您打开了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. If you don't care about this, though, then it's not a problem.

这两款产品都不支持选择功能。最终您将得到对象的列表/集合,而不是您想要的类型。如果可能,我建议避免在xsd中进行选择,因为这不会很好地将/反序列化到强类型类中。如果你不关心这个,那就没问题了。

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

xsd2code中的any特性将反序列化为System.Xml。XmlElement,我觉得它非常方便,但如果你想要强类型对象,它可能是个问题。当允许自定义配置数据时,我经常使用any,因此XmlElement可以方便地传递到其他地方自定义的另一个XML反序列化器。

#4


1  

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%

#5


1  

Marc Gravells answer was right for me but my xsd was with extension of .xml. When I used xsd program it gave :
- The table (Amt) cannot be the child table to itself in nested relations.

Marc Gravells的回答对我来说是正确的,但我的xsd使用了.xml扩展名。当我使用xsd程序时,它给出了:-表(Amt)不能是嵌套关系中的子表。

As per this KB325695 I renamed extension from .xml to .xsd and it worked fine.

根据KB325695,我将扩展名从.xml重命名为.xsd,效果很好。

#6


0  

I used xsd.exe in the Windows command prompt.

我使用了xsd。在Windows命令提示符中的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

\程序文件(x86)\微软SDKs\Windows\v8.1A\bin\NETFX 4.5.1工具\xsd。exe”/类/语言:c。xsd xlink。xsd xml.xsd