Search code examples
c#xml-serializationxsdxsd.exexsd2code

XML complexType with element that ends up as "XmlElement"


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)


Solution

  • 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?