简体   繁体   English

设置nillable = true并在XML文档中使用默认属性是否有效?

[英]Is it valid to set nillable=true and use the default property in an XML document?

Using C# and .Net 4.0 使用C#和.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: 当我使用null值序列化对象时,我得到:

<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'." 但我得到一个反序列化:“当属性为'xsi:nil'并且值为'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. 当我查看XML规范时,我没有看到nillable和default属性是互斥的,但是我的其他dateTime XML类型是可以为空但没有默认属性正常工作。

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); 我认为这可能是XML Schema规范中令人困惑的领域之一(有趣的是,即使1.1规范也只是不同于默认和固定组合); 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? 如果你认为元素的默认值在元素存在且空的时候应用 ,并且nilled元素必须没有任何内容,那么混淆是有意义的......哪一个是:null或默认值? In other words, when both conditions are present, which one takes precedence? 换句话说,当两个条件都存在时,哪一个优先? I guess the deserializer is kind of asking for help there... 我想解串器有点在那里寻求帮助......

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. 错误消息描述了规范中存在的约束:验证规则:元素本地有效(元素)子句3.3.2表示当xsi:nil = true时,必须没有固定值。 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; 我认为你的模式的正确行为是:如果元素为空并且xsi:nil不存在或为false,则使用默认值; if the element is empty and xsi:nil is true, leave it as is. 如果元素为空且xsi:nil为true,则保持原样。

(you can try getting Microsoft to fix this, or you can try switching to Saxon...) (您可以尝试让Microsoft解决此问题,或者您可以尝试切换到Saxon ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM