XML,这是什么:null或空元素? [英] XML, what is this: null or empty element?

查看:172
本文介绍了XML,这是什么:null或空元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我的另一个问题: XML反序列化null元素吗?



我从第三方服务器获得了类似的元素用于API测试:

  < Taxable /> 
< DefaultPurchasePrice />

我刚刚意识到,现在我对这样的元素表示空对象还是空感到困惑。



谈论对象,它们是相同的,null对象通常意味着空对象引用对吗?但是尝试将XML元素映射到数据字段/值时,它们可能会有所不同,即null字符串为空字符串,但是对于Decimal price或Boolean值,它们是未定义的,等于空,但不会为null,除非已定义它们为可空值。



同样,我的XmlSerializer的问题是无法处理这样的空元素。我可以轻松地在我的代码中修复它吗?还是我应该问人们提供的XML是否具有定义明确的XML?因为似乎像这样的空XML元素是未定义的:它在这里,但是可以为null或空对XML元素本身无关紧要?但是对于该元素,除非我将所有C#类数据字段都设置为字符串类型,否则我的代码需要弄清楚该如何处理。否则,如果我的代码尝试将空或空XML元素直接映射到某个数据字段,则它肯定会失败。



我不得不问这个问题,因为我遇到了XML有很多这样的元素
,对于那些特殊元素,我的.NET XMLserialization代码需要将这些字段映射为字符串,如果字符串不为空,则将它们设置为对应的数据类型,否则将它们设置为null。最后,在进行反序列化之前,请删除那些空元素,因为它要容易得多。但是我确实在徘徊:我在代码中实际上在做什么?我只是删除了null元素还是空元素?因为它们明显不同!。但是编写XML的人认为它们是相同的,因为XML本身没有 null的概念,有人认为决定它为null还是为空是我的责任,但是XML确实使您能够以更清晰的方式表示 null元素



编辑:



在我展示的示例中,这两个元素显然应该为null而不是空元素XML实际上没有null的概念,但是这些元素可以省略(如果它们为null,则不要将它们放入XML)或使用更好的表示形式(如@svick所述),或者在其他情况下,应在有意义的情况下使用空元素,但不能用于Decimal或Boolean。

解决方案


< Taxable />

< DefaultPurchasePrice />



这是什么:null或空元素?


它是空的。在语义上与以下内容相同:

 < Taxable>< / Taxable> 
< DefaultPurchasePrice>< / DefaultPurchasePrice>

XML本身存在的唯一空概念是一个缺少的元素。您不能使用XML的语义指定元素 并说它为空。您必须具有某种模式(显式模式,例如DTD,XSD或Schematron,或隐式/逻辑模式)才能做到这一点。



限制解释XML的应用程序或官方XML相关技术。例如,XSD具有 xsi:nil = true 属性。即使您可以将其中一些架构属性应用于XML,这些属性也不是纯 XML;它们是XSD架构提供的补充。



在这种情况下,XSD可以自由地在基本XML结构之上构建自己的语义,并添加显式的 nil 不存在的地方。这种灵活性被认为是XML的优势。


Regarding to my other question: XML deserialize null elements?

I've got elements like these from a third-party server for API testing:

<Taxable />
<DefaultPurchasePrice />

I just realized that now I am confusing myself about whether elements like that represent null object or empty.

Talking about objects, they are same, null object normally means empty object reference right? But trying to map a XML element to a datafield/value, they could be different i.e null string is empty string, but for Decimal price or Boolean value, they are undefined which equal to empty, but will not be null, unless they are defined as nullable.

Again, the problem with my XmlSerializer is that wouldn't handle transfer an empty element like that. could I easily fix that in my code. Or should I ask people provide the XML have a well defined XML? Because seems that an empty XML element like that is undefined: it is here, but could be either null or empty doesn't matter for XML element itself? But for that element my code need figure out how to deal with that, unless I set all my C# class datafield as string type. Otherwise, if my code trying to directly map an empty or null XML element to a certain datafield, it will fail for sure.

I have to ask this question because I encountered XML have lots of those elements and for those special elements, my .NET XMLserialization code need to mapped those field as string and if the string is not empty, I case them into correspondent datatype, otherwise I set them to null. And I end up remove those empty element before I do my deserialization because it is much easier. But I do wandering: "What am I really doing in my code? Did I just remove null elements or empty elements? Because they are clearly different! But people writing that XML think they are same, because XML itself don't have concept of 'null', and some people argue that it is my responsibility to decide whether it is null or empty. But XML do enable you to represent 'null' elements in a clearer way

Edit:

In the example I presented, those two elements clearly should be null rather than empty elements. XML don't really have a concept of null, but those elements could either be omitted (if they are null, don't put them into XML) or using better representation as mentioned be @svick. Or in other cases, empty elements should be used when they make sense. But not for Decimal or Boolean.

解决方案

<Taxable />
<DefaultPurchasePrice />

what is this: null or empty element?

It is empty. It is semantically the same as:

<Taxable></Taxable>
<DefaultPurchasePrice></DefaultPurchasePrice>

The only "null" concept that exists in XML itself is a missing element. You can't specify an element and say that it is null, using XML's semantics. You must have some sort of schema (an explicit schema, such as DTD, XSD, or Schematron, or an implicit/logical schema) to do this.

This does not restrict an application that interprets the XML, though, or official XML-related technologies. For example, XSD has an xsi:nil="true" attribute. Even though you can apply some of these schema attributes to the XML, those attributes aren't "pure" XML; They are an addition supplied by XSD schemas.

This is a case where XSD was free to build its own semantics on top of the basic XML structure, and add an explicit nil where one didn't exist. This flexibility is considered an advantage of XML.

这篇关于XML,这是什么:null或空元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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