Generated code does not match schema

Oct 20, 2011 at 4:07 PM

I might be doing something totally wrong, but i created a simple test schema:

 

<?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:choice>
				<xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
					<xs:complexType>
						<xs:choice>
							<xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
						</xs:choice>
						<xs:attribute name="SomeAttribute" type="xs:string"/>
						<xs:attribute name="SomethingElse" type="xs:string"/>
					</xs:complexType>
				</xs:element>
				<xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
			</xs:choice>
		</xs:complexType>
	</xs:element>
</xs:schema>

 

One root, two children (one optional).

I ran the Xsd2Code from VS2010 and the generated code created two "root" classes (MyRoot and MyChildOne) without creating the expected MyChildTwo. I would have expected a model with MyRoot.MyChildOne...

Here the generated code:

 

    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Collections.Generic;


    public partial class MyRoot
    {

        private List<object> itemsField;

        public MyRoot()
        {
            this.itemsField = new List<object>();
        }

        public List<object> Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }

    public partial class MyRootMyChildOne
    {

        private List<object> itemsField;

        private string someAttributeField;

        private string somethingElseField;

        public MyRootMyChildOne()
        {
            this.itemsField = new List<object>();
        }

        public List<object> Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }

        public string SomeAttribute
        {
            get
            {
                return this.someAttributeField;
            }
            set
            {
                this.someAttributeField = value;
            }
        }

        public string SomethingElse
        {
            get
            {
                return this.somethingElseField;
            }
            set
            {
                this.somethingElseField = value;
            }
        }
    }

 

I don't understand how can I serialize this into a valid (schema compliant) XML file...

 

Thanks for educating me on this

Cos