引用通用类型的 XSD Gen 类

发布于 2024-12-18 13:54:46 字数 1913 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-12-25 13:54:47

您可以将 XSD.exe 与多个文件参数一起使用:

xsd .\XSD\Common.xsd .\XSD\Employee.xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"

You can use XSD.exe with multiple file arguments:

xsd .\XSD\Common.xsd .\XSD\Employee.xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
绝不放开 2024-12-25 13:54:47

您基本上希望将通用类型拆分为一个通用程序集,该程序集可供其他类型引用。您有两个选择:

  1. 手动将它们拆分。我知道这是一个生成的文件,但如果您的源模式相当静态,那么这只是一次性的练习。
  2. 请改用 svcutil.exe。然而,这要复杂得多,实际上,除非您的模式都遵守某些准则,否则您甚至可能无法做到这一点。如果您有兴趣,请参阅下面的过程。

如果您喜欢上面的选项 2,那么一般过程如下:

  1. 使用 svcutil 上的 /dconly 标志从 Common.xsd 中提取类型。这将生成一个包含常见类型的类文件。
  2. 将此类编译为程序集
  3. 使用 /r 标志并引用 CommonTypes.dll 程序集从 A.xsd 中提取类型。
  4. 对 B.xsd 执行相同的操作

。但是,此方法基于使用 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:

  1. Manually split them out. I know this is a generated file but if your source schemas are fairly static then it's a one off exercise.
  2. Use svcutil.exe instead. However this is much more complicated and actually you may not even be able to do this unless your schemas all abide by certain guidelines. If you are interested see below for the process.

If you fancy option 2 above then here is the general process:

  1. Extract the types from Common.xsd using the /dconly flag on svcutil. This will generate a class file with your common types.
  2. Compile this class into an assembly
  3. Extract the types from A.xsd using the /r flag and referencing your CommonTypes.dll assembly.
  4. Do the same for B.xsd

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文