XML解析器是否区分了xsi:nil =" true"并省略了元素? [英] Do XML parsers tell the difference between xsi:nil="true" and omitted elements?

查看:162
本文介绍了XML解析器是否区分了xsi:nil =" true"并省略了元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML解析器/反序列化器通常能够区分 nillable elements显式设置为null和省略的可选元素

Are XML parsers/deserializers in general able to tell the difference between nillable elements explicitly set to null and optional elements that are left out?

假设我们有以下复杂类型:

Assume that we have the following complex type:

<complexType name="NiceType">
  <sequence>
    <element name="niceElem" nillable="true" type="int" minOccurs="0" />
  </sequence>
</complexType>

元素显式设置为null(示例1):

Element explicitly set to null (example 1):

<niceType>
  <niceElem xsi:nil="true"/> 
</niceType>

省略元素(示例2):

<niceType>
</niceType>

一般是解析器,例如JAX-B实现或.NET等,例如XML模块WCF,能够分辨出上面的示例1和示例2之间的区别吗?换句话说,您是否能够以可互操作的方式组合两个NULL表示 - 如示例中所示 - 以便传达不同的NULL阴影?

Would parsers in general, such as JAX-B implementations or .NET alikes such as the XML module of WCF, be able to tell the difference between example 1 and example 2 above? In other words, would you in an interoperable manner be able to combine both NULL representations - as in the example - in order to communicate different shades of NULL?

推荐答案

XML解析器(例如 XmlReader XmlDocument XDocument )不要特意处理 xsi:nil - 你仍然会看到流/文档中的元素。

XML parsers (e.g. XmlReader, XmlDocument, XDocument) don't treat xsi:nil specially - you still see the element in the stream/document.

XmlSerializer 确实处理 xsi:nil :在该上下文中它意味着与省略的节点相同;您可以使用 XmlSerializer 使用 XmlSerializerFormatterAttribute DataContract 来使WCF序列化$ c>。

XmlSerializer does handle xsi:nil: within that context it means the same thing as an omitted node; you can make WCF serialize using XmlSerializer by marking your DataContracts using XmlSerializerFormatterAttribute.

DataContractSerializer 确实使用了该属性:但是我不确定它的所有规则是什么使用它们(一个案例是循环引用) - 它更可能省略元素。我不认为您应该将 xsi:nil 传递给 DataContractSerializer ,除非它在这种情况下使用它 - 如 DataContractSerializer 是围绕假设设计的,以提高de /序列化性能。

DataContractSerializer does use the attribute: however I am not sure what all the rules are for it to use them (one case is circular references) - it is much more likely to omit elements. I don't think you should pass xsi:nil to DataContractSerializer unless it uses it in that case - as DataContractSerializer is designed around assumptions to improve de/serialization performance.

来自规范看起来它原本设计为像JavaScript一样工作 null undefined - 其中 null xsi:nil )是一个有效值, undefined (省略)是一个值的完整不存在;特别是复杂类型 - 你可以提供元素但省略它的内容(即使根据模式需要内容)。

From the spec it looks like it was originally designed to work like the JavaScript null and undefined - where null (xsi:nil) is a valid value and undefined (omitted) is the entire non-existence of a value; especially with complex types - you can provide the element but omit it's content (even if the content is required according to the schema).

一般情况下我会避免它。这是非直观的 - 我认为我没有看到使用它的REST / SOAP API(除了专门使用它的InfoPath);大多数只使用 null = undefined 。 xmlns声明和它的用法也会占用一些额外的宝贵字节。

In general I would avoid it. It's non-intuitive - I don't think I have seen a REST/SOAP API out there that uses it (except InfoPath which uses it exclusively); most just use null = undefined. The xmlns declaration and usage of it also eat a few extra valuable bytes.

奖励标记:如果你使一个元素可选而且它不是'可以为空(例如 xsd:int )C#生成器提供< Name>指定的属性 - 你可以添加你的拥有这样的属性。这将允许您区分 xsi:nil 和省略(指定时为nil,null,未指定时省略)。但是,这仅适用于 XmlSerializer

Bonus Marks: If you make an element optional and it isn't nullable (e.g. xsd:int) the C# generator provides a <Name>Specified property - you can add your own properties like this. This would allow you to differentiate between xsi:nil and omittance (nil when specified and null, omitted when not specified). However, this only works with XmlSerializer.

这篇关于XML解析器是否区分了xsi:nil =&quot; true&quot;并省略了元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆