views:

249

answers:

1

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"&gt;
  <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)

A: 

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?

John Saunders
Well, I was trying to put in as little of the XSD as possible because otherwise this page will get huge and no one will look at it, but apparently this isn't enough. The XSD does validate, and the XML is valid.Actually I'm not sure you're right anyway sorry, because the "<choice>" means that only one of them is required.
Paul
Well, yes, now that you post the XSD! You're right. A command could be a login or a logout.
John Saunders
It had the <choice> element even before I edited it.Anyway, it's completely beside the point. That wasn't my issue, my issue was how to create a class that is of type "XmlElement" which the serializer is expecting
Paul
That's the http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.aspx class. Let me know if you still need help after that.
John Saunders