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
If you use xsd.exe to generate this class, it gives you the same thing:
Except for the use of the known type declarations:
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:
This gives us the following XML: