XSD Gen Classes That Reference a Common Type

前端 未结 2 2006
鱼传尺愫
鱼传尺愫 2021-02-06 01:08

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

相关标签:
2条回答
  • 2021-02-06 01:45

    You can use XSD.exe with multiple file arguments:

    xsd .\XSD\Common.xsd .\XSD\Employee.xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
    
    0 讨论(0)
  • 2021-02-06 01:46

    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.

    0 讨论(0)
提交回复
热议问题