gpt4 book ai didi

c# - 创建 XSD 以验证 C# 标志枚举

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:52 25 4
gpt4 key购买 nike

我有一个可以执行的命令的标志枚举

[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 以反射(reflect)我的 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>

关于c# - 创建 XSD 以验证 C# 标志枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27829985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com