2012-08-27 43 views
8

我用XSD生成器得到了一個奇怪的行爲,我無法真正解釋。我有一個這樣的XSD:當生成C#代碼時,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 

所以到「原始」屬性「指定」被追加和類型現在布爾。 任何人都可以解釋爲什麼這樣嗎?

回答

9

bool屬性表示相關屬性應該被序列化。

例如

如果boolMinDuration_100msSpecified設置爲false,並設置了MinDuration_100ms300,當您使用XmlSerializer序列化對象時,MinDuration_100ms屬性不會被序列化。

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

+3

謝謝,有沒有辦法阻止創建該屬性? – derape

+1

您可以嘗試提供更多功能的xsd2code工具。 http://xsd2code.codeplex.com/ –

+0

那麼我們最終得到了一個由我們自己的代碼生成器,它符合我們的需求,但是無論如何感謝:-) – derape

2

Set minOccurs =「1」其中元素是可填充的。 例如:

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

這會改變我們不想要的語義:'null'和空不一樣... – derape