Handling of optional | 0-1 lists

Jan 5, 2010 at 8:02 PM

Using V3.0.1.0 I have been unable to see how to get handling of optional attributes and elements which have a 0-1 size.

Recently added 'Lazy loading' alleviates some of the issues with construction of optionals, but because the field is not accessible you cannot check if it exists yet.

private List<dvd> dvdsField; 
[System.Xml.Serialization.XmlElementAttribute("Dvds")]
public List<dvd> Dvds {
get {
if ((this.dvdsField == null)) {
this.dvdsField = new List<dvd>();
}
return this.dvdsField;
}
set {
this.dvdsField = value;
}
}

Some other providers have added a second field (bit ugly) to identify existance

public Boolean dvdsFieldExists { get{}; }

 

I like the option provided by the CodeSynthesis XSD tool in C++ giving a generic class Optional<>


Taking the new CircularClassReference.xsd example and extending
<xs:schema xmlns:mstns="http://tempuri.org/CircularClassReference.xsd" elementFormDefault="qualified" targetNamespace="http://tempuri.org/CircularClassReference.xsd" id="CircularClassReference"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="Circular">
    <xs:sequence>
      <xs:element name="Circular" type="mstns:Circular"/>
      <xs:element name="Recursion" type="mstns:Circular" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="mstns:Root" minOccurs="0" maxOccurs="1"/>
      <xs:element name="count" type="xs:int"/>
    </xs:sequence>
    <xs:attribute name="Optional" use="optional" type="xs:boolean"/>
  </xs:complexType>
  <xs:element name="Root" type="mstns:Circular"/>
</xs:schema>