Attributes and nullable types

Sep 1, 2009 at 10:41 AM

When using attributes the generated fields don't use the nullable type. Example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    targetNamespace="http://MyProj/Things"
    elementFormDefault="qualified"
    xmlns="http://MyProj/Things"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name="Things">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Thing" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Service">
                                <xs:complexType>
                                    <xs:attribute name="Name" type="xs:string" />
                                    <xs:attribute name="Cost" type="xs:decimal" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="Name" type="xs:string" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

And the cs:

private decimal costField;

private bool costFieldSpecified;

I think it should be

 private decimal? costField;

Sep 10, 2009 at 3:13 PM
Edited Sep 10, 2009 at 3:14 PM

Surely it should only be a nullable type if the schema has marked it as being nullable.  Your schema has not specified that, namely xsi.nil  Your Thing type is nullable, not the decimal

Apr 28, 2013 at 11:04 PM
I wouldn't normally necro a thread, but I've just started using this library, and encountered this same problem.

An attribute is optional (nullable) by default, unless use="required" is specified in the schema:

http://www.w3schools.com/schema/el_attribute.asp

So surely attributes should generate nullable types by default?