13

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?

Community
  • 1
  • 1
DerApe
  • 3,097
  • 2
  • 35
  • 55

2 Answers2

12

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.

Littm
  • 4,923
  • 4
  • 30
  • 38
Frank Hu
  • 136
  • 1
  • 5
  • 3
    Thanks, is there a way to prevent the creation of that property though? – DerApe Oct 01 '12 at 05:42
  • 1
    You can try the xsd2code tools, which providing more functions. http://xsd2code.codeplex.com/ – Frank Hu Nov 02 '12 at 01:53
  • Well we ended up with a codegen by ourselfs which fits our needs, but thanks anyway :-) – DerApe Nov 05 '12 at 07:32
  • @derape - I have a similar requirement as you had. Would it possible for you to share the code generator that you used? – HelpMatters Sep 01 '14 at 05:26
  • @vivekp Our generator is designed to fit our specific needs (external dependencies etc.). So it would not make sense to do so. Also its companies property. But if you want to, I could give you directions if you need help creating your own... – DerApe Sep 01 '14 at 05:41
  • @derape - Yes that makes sense. Thanks a lot. Here's my question which talks about my problem. http://stackoverflow.com/questions/25599268/in-c-sharp-how-do-i-ignore-all-the-attributes-with-the-postfix-specified-gener The question is simple. Is there a way not to have properties with "Specified" Postfix at all using xsd.exe or something similar. – HelpMatters Sep 01 '14 at 05:43
2

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

<xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="1" />
Ilya Lysenko
  • 1,772
  • 15
  • 24
  • 3
    That would change the semantical meaning which we did not want: `null` and empty is not the same... – DerApe Dec 15 '14 at 09:45