我有一个可以执行的命令的标志枚举
[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值,因为创建者不会知道int值应该是什么。
我希望我的C#代码尽可能保持不变,并重新设计我的XSD以反映我的C#代码应该是什么。现在,VS2013生成的一些生成的文本XML具有多个不同的命令元素。我知道这就是我的XSD写法,但这不是我想要的。我想要一个可以在枚举中包含任何字符串的元素。我如何设置这个XSD以及这个实现发送多个不同命令的示例XML是什么样的。
答案 0 :(得分: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>