c# - 当 minOccurs 属性 > 1 时使用默认属性有什么好处?

标签 c# xml xsd xml-serialization

我遇到了一个遗留的 XSD,它有一堆像......

      <xs:element minOccurs="1"
                  maxOccurs="1"
                  default="true"
                  name="Ready" type="xs:boolean" />

...其中 minOccurs 和 maxOccurrs 都等于 1,并且有一个默认值。由于 XML 文档无论如何都需要包含一些值,因此在此处使用默认属性有什么好处吗?

最佳答案

来自spec :

The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

我认为这意味着一个空的 <Ready />根据您的架构,节点将默认为 TRUE 值。

关于c# - 当 minOccurs 属性 > 1 时使用默认属性有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1750476/

相关文章:

c# - 如何将 Xamarin.Forms 条目绑定(bind)到非字符串类型,例如 Decimal

c# - XML XNA 对象反序列化

mysql - 如何执行这个 LOAD XML INFILE SQL 查询?

xml - 如何在 XML 模式中实现互斥属性?

c# - 从 TTF 字体流创建 GlyphTypeface 对象

c# - 'MyIdentityModels.User' 违反了类型 'TUser' 的约束

c# - 助手与 C# 类?什么时候用什么

c# - 如何在 LINQ to XML 中选择具有命名空间的 XML 节点

java - 带有 JAXB 的 WebService - 参数包含 null

wcf - 定义 WCF XML 响应架构