具有最终为“XmlElement”的元素的 XML 复杂类型
在我的 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)。 但是,如果我希望 d 提交注销命令,则 XML 需要如下所示:
<command>
<logout />
</command>
但是,我还没有 - 任何相当于 a 的内容 - logoutType。 在生成的类中,如果我想使用注销,则 commandType 需要一个“XmlElement”。
假设 XSD 到 C# 工具无法为我生成此类,那么如何编写一个基本上只序列化为 XmlElement 类型并适合 commandType 的类?
(注意:我无法控制 XSD,否则我会更改它以包含新的复杂类型)
In my XSD, I have something similar to this:
<?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>
So, using an XSD to C# tool (xsd.exe or Xsd2Code), this generates 2 classes (commandType and loginType). But, if I wanted dto submit a logout command, the XML needs to look like this:
<command>
<logout />
</command>
But, I haven't got - whatever the equivalent of a - logoutType. In the generated class, if I wanted to use logout, then commandType is expecting an "XmlElement".
Assuming the XSD to C# tools can't generate this class for me, how do you write a class that basically comes down to just serializing to and is of type XmlElement so it fits with the commandType?
(note: I have no control over the XSD's, otherwise I would have changed it to include a new complexType)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据现在发布的架构,很清楚为什么您有一个用于
logout
的XmlElement
。 您认为logout
元素的类型是什么? 它是xs:anyType
。 它可以是任何东西。 唯一匹配的 .NET 类型是XmlElement
,除非您更喜欢object
。您想要什么而不是
XmlElement
?Based on the schema that has now been posted, it's clear why you have an
XmlElement
forlogout
. What do you think the type of thelogout
element is? It'sxs:anyType
. It could be anything at all. The only .NET type that matches that isXmlElement
, unless you preferobject
.What did you want instead of
XmlElement
?