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)。但是,如果我想 dto 提交注销命令,XML 需要如下所示:

<command>
    <logout />
</command>

但是,我没有 -任何相当于- logoutType。在生成的类中,如果我想使用注销,那么 commandType 需要一个“XmlElement”。

假设 XSD 到 C# 工具不能为我生成这个类,你如何编写一个基本上归结为只是序列化并且是 XmlElement 类型的类,以便它适合 commandType?

(注意:我无法控制 XSD,否则我会更改它以包含新的 complexType)

4

1 回答 1

0

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

你想要什么而不是XmlElement

于 2009-08-03T22:55:51.003 回答