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

DerApe picture DerApe · Aug 27, 2012 · Viewed 7.1k times · Source

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?

Answer

Frank Hu picture Frank Hu · Sep 30, 2012

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.