【问题标题】:XSD tool appends "Specified" to certain properties/fields when generating C# codeXSD 工具在生成 C# 代码时将“指定”附加到某些属性/字段
【发布时间】:2012-08-21 17:34:07
【问题描述】:

XSD 生成器出现了一个我无法解释的奇怪行为。我得到了这样的 XSD:

<xs:complexType name="StageSequenceElement" mixed="false">
    <xs:complexContent>
        <xs:extension base="CoreObject">
            <xs:sequence>
                <xs:element name="Description" type="xs:string" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Some Doc</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="StageRef" type="ObjectReference">
                    <xs:annotation>
                        <xs:documentation>...</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="MinDuration_100ms" type="xs:int" nillable="true" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>...</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="MaxDuration_100ms" type="xs:int" nillable="true">
                    <xs:annotation>
                        <xs:documentation>...</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>...</xs:documentation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

它是从CoreObject派生的:

<xs:complexType name="CoreObject">
    <xs:sequence>
        <xs:element name="No" type="xs:int">
            <xs:annotation>
                <xs:documentation>...</xs:documentation>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>

这只是 XSD 的一小部分,还有很多更复杂的类型。

所以当我生成类似于this 的类时,我得到了一个生成的类,它还有两个属性(除了我期望的 5 个属性):

public bool MinDuration_100msSpecified

public bool StageOnDemandSpecified

所以“原始”属性“指定”被附加,类型现在是布尔值。 谁能解释一下为什么会这样?

【问题讨论】:

    标签: c# code-generation xsd.exe


    【解决方案1】:

    bool 属性表示相关属性应该被序列化。

    例如

    如果bool MinDuration_100msSpecified 设置为false,并且您将MinDuration_100ms 设置为300,当您使用XmlSerializer 序列化对象时,MinDuration_100ms 属性将不会被序列化。

    此功能可以将序列化的xml文件保存到最小。

    【讨论】:

    • 谢谢,有没有办法阻止创建该属性?
    • 你可以试试xsd2code工具,它提供了更多的功能。 xsd2code.codeplex.com
    • 好吧,我们最终得到了一个符合我们需求的代码生成器,但无论如何谢谢:-)
    • @derape - 我有和你类似的要求。您可以分享您使用的代码生成器吗?
    • @vivekp 我们的生成器旨在满足我们的特定需求(外部依赖等)。所以这样做是没有意义的。也是其公司财产。但如果你愿意,如果你需要帮助创建自己的,我可以给你指示......
    【解决方案2】:

    设置 minOccurs="1" 其中元素是可空的。 例如:

    <xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="1" />
    

    【讨论】:

    • 这会改变我们不想要的语义含义:null 和空不一样...
    猜你喜欢
    • 2021-04-06
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2011-03-13
    • 1970-01-01
    • 2015-06-19
    • 2012-03-16
    相关资源
    最近更新 更多