0

Using C# and .Net 4.0

I have a generated schema that looks like this:

<xsd:element name="EstimatedDate" minOccurs="0" nillable="true" default="1900-01-01T00:00:00">
  <xsd:complexType>
       <xsd:simpleContent>
           <xsd:extension base="xsd:dateTime">
                <xsd:attribute name="origVal" type="xsd:dateTime" use="optional" />
           </xsd:extension>
       </xsd:simpleContent>
  </xsd:complexType>

When I serialize the object with a null value I get:

<EstimatedDate xsi:nil="true" />

But I am getting a deserialization: "There must be no fixed value when an attribute is 'xsi:nil' and has a value of 'true'."

When I look at the XML specification I do not see that nillable and default properties are mutually exclusive, but my other dateTime XML types that are nillable but do not have a default property work correctly.

Steve
  • 1,557
  • 2
  • 17
  • 34

2 Answers2

1

I think that maybe it is one of those confusing areas in the XML Schema specification (interesting enough, even 1.1 spec is only dissalowing default and fixed combination); if you consider that default values for elements apply when the elements are present and empty, and that nilled elements must not have any content, then it kind of make sense to get confused... which one is it: null or default? In other words, when both conditions are present, which one takes precedence? I guess the deserializer is kind of asking for help there...

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • That's kind of what I am thinking. Having a default with a nillable makes little sense. I'll probably just have to code around the schema definition since I have no control over it in my project. – Steve Dec 09 '11 at 01:47
1

The error message describes a constraint that is present in the spec: Validation Rule: Element Locally Valid (Element) clause 3.3.2 says that when xsi:nil=true, there must be no fixed value. However, there is no ban on a default value, as far as I can see, so it seems your schema processor is over-eager to find fault.

I think the correct behavior for your schema is: if the element is empty and xsi:nil is absent or false, use the default value; if the element is empty and xsi:nil is true, leave it as is.

(you can try getting Microsoft to fix this, or you can try switching to Saxon...)

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Hi Michael, could you please explain how Saxon would help here - I assumed we were talking about deserialization of an XML document into a set of C# classes. I confirmed that this is not a problem with .NET's XML Schema processor since the above XML validates fine with the built in implementation... – Petru Gardea Dec 09 '11 at 15:09
  • Sorry, I misunderstood, I thought your problem was with the Microsoft XSD processor. I'm afraid data binding tools are often less than 100% compliant with the XSD specifications. – Michael Kay Dec 11 '11 at 19:24
  • @MichaelKay is there a reason why an element can be declared as both nillable and fixed? – yas Mar 04 '22 at 16:15
  • 1
    Please don't ask new questions as comments on a ten-year old answer. Raise a new question. – Michael Kay Mar 05 '22 at 09:44