Xsd choice codes

Apr 12, 2010 at 6:42 PM

I created a sample XSD with choice inside.

When I used the tool to generate classes I can get Customer and Address types directly under MyRoot class; while SubObject has "choice" and generated code has object array instead of strongly typed object nested. This makes code kind of inconnevient because developers have to memorize the sequence defined in XSD exactly, otherwise the serialized XML may not be compatible with XSD.

My question is that is there a way the tool can generated strongly typed objects inside choice as well?

    public partial class MyRootSubObject {
        
        private object[] itemsField;
        
        private ItemsChoiceType[] itemsElementNameField;
        
        [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
        
        public ItemsChoiceType[] ItemsElementName {
            get {
                return this.itemsElementNameField;
            }
            set {
                this.itemsElementNameField = value;
            }
        }
    }

 

Visualized XSD definition

 XSD Content

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="MyRoot">
		<xs:annotation>
			<xs:documentation>Comment describing your root element</xs:documentation>
		</xs:annotation>
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Customer" type="CustomerType"/>
				<xs:element name="Address" type="AddressType" minOccurs="0"/>
				<xs:element name="SubObject">
					<xs:complexType>
						<xs:choice>
							<xs:sequence>
								<xs:element name="ContactId" type="xs:string"/>
								<xs:element name="ContactPerson" type="CustomerType" minOccurs="0"/>
							</xs:sequence>
							<xs:sequence>
								<xs:element name="AddressId" type="xs:string"/>
								<xs:element name="MailingAddress" type="AddressType" minOccurs="0"/>
							</xs:sequence>
						</xs:choice>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="CustomerType">
		<xs:sequence>
			<xs:element name="FirstName" type="xs:string"/>
			<xs:element name="LastName" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="AddressType">
		<xs:sequence>
			<xs:element name="StreetAddress" type="xs:string"/>
			<xs:element name="City" type="xs:string"/>
			<xs:element name="State" type="xs:string"/>
			<xs:element name="ZipCode" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

Aug 6, 2011 at 3:32 AM

I am facing the same problem. Is there any solution for this?

Thanks

 

Aug 14, 2011 at 8:22 AM

No, there are no solution for the moment. With xsd.exe it's the same problem.

This is one limitation of xsd2code.

Pascal.