问题描述
我希望能够从 XSD 文件生成 DataContract
,最好使用 xsd.exe 工具.在我的每个项目上自动生成 [DataContract]
和 [DataMember]
的最简单方法是什么?
I want to be able to generate a DataContract
from a XSD file, preferably using the xsd.exe tool. What is the easiest way for it to auto generate the [DataContract]
and [DataMember]
on each of my items?
或者有更好的方法吗?我试图避免每次更改和重新生成 XSD 文件时都必须重新创建数据协定.
Or is there a better approach? I am trying to avoid having to recreate the data contract each time the XSD file is changed and regenerated.
推荐答案
xsd.exe
工具早于 WCF 并且对 [DataContract]
和 一无所知代码>[数据成员].如果您确实使用
xsd.exe
,则必须切换 WCF 以使用 XmlSerializer
而不是它的默认 DataContractSerializer
用于序列化数据协定.
The
xsd.exe
tool predates WCF and doesn't know anything about [DataContract]
and [DataMember]
. If you do use xsd.exe
, you'll have to switch WCF to use the XmlSerializer
instead of its default DataContractSerializer
for serializing the data contracts.
xsd.exe
的 WCF 等效项是 svcutil.exe
- 它有一个参数 /dconly
,它仅从给定的 XSD 文件创建数据协定.这将为您生成一个 C# 或 VB.NET 文件,其中包含很好地注释的数据协定.
The WCF equivalent for
xsd.exe
is svcutil.exe
- it has a parameter /dconly
which creates the data contracts only, from a given XSD file. This will generate a C# or VB.NET file for you, containing the data contracts nicely annotated.
用法:
svcutil.exe (name of your XSD).xsd /dconly
这将在您的目录中生成一个具有相同基本名称的 *.cs 文件.
This would generate a *.cs file by the same base name in your directory.
根据我的经验,
svcutil.exe
对其 XML 结构非常挑剔 - 因此,如果它向您发出大量警告和/或错误,请不要感到惊讶.
In my experience,
svcutil.exe
is quite picky about its XML structures - so don't be surprised if it barks at you with tons of warnings and/or errors.
这篇关于从 XSD 生成 DataContract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!