在我的 XSD 中,我有类似的东西:
<?xml version="1.0" encoding="utf-8" ?>
<schema xmlns:jump="testThingy" elementFormDefault="qualified" targetNamespace="testThingy" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="command" type="jump:commandType" />
<complexType name="loginType">
<sequence>
<element name="userName" />
</sequence>
</complexType>
<complexType name="commandType">
<sequence>
<choice>
<element name="login" type="jump:loginType" />
<element name="logout" />
</choice>
<!-- There are other elements here but they are IRRELEVANT to the question -->
</sequence>
</complexType>
</schema>
因此,使用 XSD 到 C# 工具(xsd.exe 或 Xsd2Code),这会生成 2 个类(commandType 和 loginType)。但是,如果我想 dto 提交注销命令,XML 需要如下所示:
<command>
<logout />
</command>
但是,我没有 -任何相当于- logoutType。在生成的类中,如果我想使用注销,那么 commandType 需要一个“XmlElement”。
假设 XSD 到 C# 工具不能为我生成这个类,你如何编写一个基本上归结为只是序列化并且是 XmlElement 类型的类,以便它适合 commandType?
(注意:我无法控制 XSD,否则我会更改它以包含新的 complexType)