问题
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?
回答1:
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.
回答2:
Set minOccurs="1" where element is nillable. For example:
<xs:element name="StageOnDemand" type="xs:boolean" nillable="true" minOccurs="1" />
来源:https://stackoverflow.com/questions/12138439/xsd-tool-appends-specified-to-certain-properties-fields-when-generating-c-shar