This project is read-only.

Problems with mandatory collection with optional member

May 6, 2014 at 4:36 PM
Edited May 6, 2014 at 4:37 PM

I hope this project is still under development.

According to Issue 10718 I have found a probelm with mandatory collections with optional member.

My xsd is looking like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd=""
            xmlns:test="urn:test:dev:xsd:1" elementFormDefault="unqualified"
            attributeFormDefault="unqualified" version="1.0">
  <xsd:simpleType name="ItemType">
    <xsd:restriction base="xsd:string"/>
  <xsd:complexType name="CollectionType">
          <xsd:element name="items">
                <xsd:element name="item" type="dev:ItemType" minOccurs="0"

The class CollectionType should have a mandatory list named items, but the list itself could be empty. After serealizing an instance of this class with an empty list the items element is missing therefore the xml is invalid. For better understandingh here the generated class:
/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:test:dev:xsd:1", IsNullable=true)]
    public partial class CollectionType
        private System.Collections.Generic.List<string> itemsField;
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        public System.Collections.Generic.List<string> items
                return this.itemsField;
                this.itemsField = value;
        public virtual bool ShouldSerializeitems()
            return ((this.items != null) 
                        && (this.items.Count > 0));
After i have done some researches I found out that during the serealizing process the method "ShouldSerializeitems" is called and that method suppressed the serialization of the empty list.

A little code review lead me to the class Thinktecture.Tools.Web.Services.CodeGeneration.CollectionTypeGenerator within the method HandleShouldSerialize the ShouldSerialize method will be generated based on the IsNullable argument of the XmlArrayItemAttribut. But for my understanding it doesnt matter if the list members are optional or not only if the list itself is optional the serialization should be suppressd.

Hoping for a quick response

Thanks for your help Kind Regards