引用通用类型的 XSD Gen 类
我正在使用 XSD 在 C# 中定义我的 DTO 类型。我正在使用 XSD.exe 从 XSD 生成类。
我有一个定义地址类型的 Common.xsd,我想在多个类中使用它:
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street1" type="xs:string"/>
<xs:element name="Street2" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="State" type="xs:string"/>
<xs:element name="Zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Address" type="mhm:Address"/>
我在一家公司 XSD 中引用它:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Company">
<xs:sequence>
<xs:element name="AdmCode" type="xs:string"/>
<xs:element name="CompanyCode" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element ref="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Company" type="mhm:Company"/>
还有一名员工 XSD:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="EmployeeNumber" type="xs:int"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Address" type="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Employee" type="mhm:Employee"/>
我使用此命令行生成类:
xsd .\XSD\Common.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Employee.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
当我转到编译项目,我发现Company.cs类文件和Employee.cs类文件中都生成了Address类型。
如何获得在 Common.cs 类文件中仅生成一次的地址类型,并且 Employee 和 Company 类型使用此单一地址类型?
I am using XSD's to define my DTO types in C#. I am using XSD.exe to gen the classes from the XSD's.
I have a Common.xsd that defines an Address type and I want to use this in more than one class:
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street1" type="xs:string"/>
<xs:element name="Street2" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="State" type="xs:string"/>
<xs:element name="Zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Address" type="mhm:Address"/>
I am referencing this in a company XSD:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Company">
<xs:sequence>
<xs:element name="AdmCode" type="xs:string"/>
<xs:element name="CompanyCode" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element ref="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Company" type="mhm:Company"/>
And an employee XSD:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="EmployeeNumber" type="xs:int"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Address" type="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Employee" type="mhm:Employee"/>
I gen the classes using this command line:
xsd .\XSD\Common.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Employee.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
When I go to compile the project, I find that the Address type has been generated in both the Company.cs class file and the Employee.cs class file.
How can I get the Address type generated just once in the Common.cs class file and the Employee and Company types use this single Address type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
XSD.exe
与多个文件参数一起使用:You can use
XSD.exe
with multiple file arguments:您基本上希望将通用类型拆分为一个通用程序集,该程序集可供其他类型引用。您有两个选择:
如果您喜欢上面的选项 2,那么一般过程如下:
。但是,此方法基于使用 DataContractSerializer 的 svcutil 来完成工作,因为 /r 标志不可用于 XmlSerializer。只有当您的架构遵守相当严格的 DCS 规则时,这才有效(可以在此处找到:http://msdn.microsoft.com/en-us/library/ms733112.aspx)。
如果不遵守这些规则,则 svcutil 将回退到使用不支持 /r 标志的 XmlSerializer。
You basically want to split out the common types into a common assembly which is references by your other types. You have two options:
If you fancy option 2 above then here is the general process:
However, this approach is based on svcutil using the DataContractSerializer to do the work, as the /r flag is not available to XmlSerializer. And this will only work if the your schemas adhere to the rather strict DCS rules (can be found here: http://msdn.microsoft.com/en-us/library/ms733112.aspx).
If these rules are not adhered to then svcutil will fall back to using XmlSerializer which does not support /r flag.