将xsd枚举转换为C#

我有一个xsd文件,我从中生成一个C#类.为了便于维护,我想在xsd文件中定义一个枚举,这样当我必须更改枚举时,我只需要在一个地方更新它.我知道如何创建枚举,但是当生成C#代码时,我需要枚举成员拥有自定义值,因此结果类似于:
public enum SetupTypeEnum {
    None = 0,NewInstall = 1,Modify = 2,Upgrade = 4,Uninstall = 8
}

有没有办法写xsd来实现这个目标?

解决方法

您可以将特定于代码生成的注释(这仅适用于svcutil,而不适用于xsd.exe)添加到xsd文件中.你的枚举的xsd定义是这样的:
<xs:simpleType name="SetupTypeEnum">
    <xs:restriction base="xs:string">
        <xs:enumeration value="None">
            <xs:annotation>
                <xs:appinfo>
                    <EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</EnumerationValue>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
        <xs:enumeration value="NewInstall">
            <xs:annotation>
                <xs:appinfo>
                    <EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1</EnumerationValue>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
        ...
    </xs:restriction>
</xs:simpleType>

这些注释允许您显式定义每个枚举值的数值.如果搜索“EnumerationValue”,可以找到示例on this msdn page.

更新:John Saunders在他的评论中正确地指出,如果您使用xsd.exe,这不起作用.但是,如果使用svcutil.exe创建C#代码,则注释将起作用.

使用svcutil.exe的示例:

svcutil /dconly "D:\test.xsd" /o:"D:\test.cs"

如果使用svcutil而不是xsd.exe,那么生成代码会略有不同.最重要的区别是svcutil将为DataContractSerialization而不是XmlSerialization生成属性.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...