1
Vote

Wrong XML generated when an element is a ComplexType

description

Schema is defined as this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="PDS">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Person" type="PersonType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="PersonType">
    <xs:sequence>
        <xs:element name="PersonIdentifier" minOccurs="0">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="8"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:schema>
 
Code to generate the XML is:
var p = new PDS();
p.Person = new System.Collections.ObjectModel.ObservableCollection<PersonType>();
p.Person.Add(new PersonType { Name = "a", PersonIdentifier = "123" });
p.Person.Add(new PersonType { Name = "b", PersonIdentifier = "456" });
p.SaveToFile(@"c:\temp\out.xml");   Which generates this XML:
<?xml version="1.0"?>
<PDS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<PersonType>
  <Name>a</Name>
  <PersonIdentifier>123</PersonIdentifier>
</PersonType>
<PersonType>
  <Name>b</Name>
  <PersonIdentifier>456</PersonIdentifier>
</PersonType>
</Person>
</PDS>
 
Which is totally wrong.
 
It should be:
<?xml version="1.0"?>
<PDS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<PersonIdentifier>123</PersonIdentifier>
</Person>
<Person>
  <PersonIdentifier>456</PersonIdentifier>
</Person>
</PDS>
 
Please advice and fix ?

comments