具有最终为“XmlElement”的元素的 XML 复杂类型

发布于 2024-07-29 23:48:26 字数 1232 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-08-05 23:48:26

根据现在发布的架构,很清楚为什么您有一个用于 logoutXmlElement。 您认为 logout 元素的类型是什么? 它是xs:anyType。 它可以是任何东西。 唯一匹配的 .NET 类型是 XmlElement,除非您更喜欢 object

您想要什么而不是 XmlElement

Based on the schema that has now been posted, it's clear why you have an XmlElement for logout. What do you think the type of the logout element is? It's xs:anyType. It could be anything at all. The only .NET type that matches that is XmlElement, unless you prefer object.

What did you want instead of XmlElement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文