Search code examples
xsdcode-generationxsd2code

Xsd2Code help - generated code does not seem to match schema


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>

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's 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


Solution

  • If you use xsd.exe to generate this class, it gives you the same thing:

    public partial class MyRoot {
    
        private object[] itemsField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))]
        [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
    }
    

    Except for the use of the known type declarations:

    [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
    

    So it makes sense if you think about it. Because your child type 2 is a string and string is a simple type in XSD you can add instances of System.String to your Items array and then serialize this out using the above code. Each string will be wrapped in a <MyChildTwo/> node.

    UPDATE

    To make this work you create your type and then use XmlSerializer:

    var root = new MyRoot();
    root.Items = new object[2];
    root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" };
    root.Items[1] = "hello";
    
    var ser = new XmlSerializer(typeof(MyRoot));
    var memoryStream = new MemoryStream();
    var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    var streamReader = new StreamReader(memoryStream, Encoding.UTF8);
    ser.Serialize(xmlTextWriter, root);
    memoryStream.Position = 0;
    
    string xml = streamReader.ReadToEnd();
    

    This gives us the following XML:

    <MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <MyChildOne SomeAttribute="test" SomethingElse="test" />
        <MyChildTwo>hello</MyChildTwo>
    </MyRoot>