2015-01-07 26 views
2

我有命令的標誌枚舉,創建XSD來驗證C#標誌枚舉

[Flags] 
public enum Operations 
{ 
    InstallNothing = 0, 

    InstallStateDatabase = 1, 
    InstallStateServer = 2, 

    InstallStoreDatabase = 4, 
    InstallStoreServer = 8, 

    InstallMaintenanceProgram = 16, 

    InstallOther=32 

} 

[XmlElement("Commands")] 
public Operations Commands { get; set; } 

我希望能夠讀取XML文件,並解析它針對XSD執行。我有我的xsd的這一部分試圖處理驗證,但我不認爲這是正確的。

<xs:element name="commands" maxOccurs="1" minOccurs="1"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="command" minOccurs="1" maxOccurs="unbounded" > 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
       <xs:enumeration value="InstallNothing" /> 
       <xs:enumeration value="InstallStateDatabase" /> 
       <xs:enumeration value="InstallStateServer" /> 
       <xs:enumeration value="InstallStoreDatabase" /> 
       <xs:enumeration value="InstallStoreServer" /> 
       <xs:enumeration value="InstallMaintenanceProgram" /> 
       <xs:enumeration value="InstallOther" /> 
       </xs:restriction> 
      </xs:simpleType> 
      </xs:element> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

的個XML要手工創建,所以我不希望存儲的int值作爲創作者不知道什麼整型值應該是。

我希望我的C#代碼在可能的情況下保持不變,並重新設計我的XSD以反映應該在我的C#代碼中出現的內容。現在,VS2013生成的一些生成的文本XML具有多個不同的命令元素。我知道這就是我寫的XSD,但這不是我想要的。我想要一個在枚舉中可以有任何字符串的元素。我如何設置這個XSD以及這個實現發送多個不同命令的示例XML是什麼樣的。

回答

2

我在 xsd select multiple values from enumeration or equivalent type找到我的答案。我之前沒有在尋找正確的東西...

我在我的命令元素中添加了一個列表。下面是更改後我的xsd:

<xs:element name="commands" maxOccurs="1" minOccurs="1"> 
     <xs:simpleType> 
     <xs:list> 
      <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:enumeration value="InstallNothing" /> 
       <xs:enumeration value="InstallStateDatabase" /> 
       <xs:enumeration value="InstallStateServer" /> 
       <xs:enumeration value="InstallStoreDatabase" /> 
       <xs:enumeration value="InstallStoreServer" /> 
       <xs:enumeration value="InstallMaintenanceProgram" /> 
       <xs:enumeration value="InstallOther" /> 
      </xs:restriction> 
      </xs:simpleType> 
     </xs:list> 
     </xs:simpleType> 
    </xs:element> 

和示例XML使用它:

<commands>InstallNothing InstallStateDatabase InstallStateServer </commands>