创建一个 XSD 来验证 C# 标志枚举
Creating an XSD to authenticate a C# flag enumeration
我有一个可以执行的命令的标志枚举
[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 是什么样的。
我在以下位置找到了答案
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>
我有一个可以执行的命令的标志枚举
[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 是什么样的。
我在以下位置找到了答案 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>