Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD tool appends "Specified" to certain properties/fields when generating C# code

I got a strange behaviour with the XSD generator I can't really explain. I got an XSD like this:

<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>

it is derived from 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>

This is just a small part of the XSD, there are a lot more complex types.

So when I generate the classes similar to this, I get a generated class which has two more properties (in addition to the 5 which I would expect):

public bool MinDuration_100msSpecified

and

public bool StageOnDemandSpecified

So to the "original" property "Specified" was appended and the type is now bool. Can anyone explain why this is so?

like image 847
DerApe Avatar asked Aug 27 '12 08:08

DerApe


2 Answers

the bool attribute means the related attribute should be serialized.

e.g.

If the bool MinDuration_100msSpecified is set to false, and you set the MinDuration_100ms to be 300, when you use XmlSerializer to serialize the object, the MinDuration_100ms attribute won't be serialized.

This feature can save the serialized xml file to be minimal.

like image 176
Frank Hu Avatar answered Nov 05 '22 12:11

Frank Hu


Set minOccurs="1" where element is nillable. For example:

<xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="1" />
like image 44
Ilya Lysenko Avatar answered Nov 05 '22 12:11

Ilya Lysenko