开发者

XML, what is this: null or empty element?

开发者 https://www.devze.com 2023-03-31 10:19 出处:网络
Regarding to my other question: XML deserialize null elements? I\'ve got elements like these from a third-party server for API testing:

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.


I think this comes down to the fact that there is no default mapping between XML and the object model of your favorite language. Consider the following XML:

<Person Name="John">
    <Father>
        <Person Name="Jim" />
    </Father>
    <Mother>
        <Person Name="Jenny" />
    </Mother>
</Person>

Does Person have properties (or fields) Father and Mother? Or does Person represent a collection that can contains objects of types Father and Mother (perhaps inheriting from abstract base type FamilyMember)? This is not clear from the XML, and it's up to you how you represent this in your object model. (The presence of schema would change that somewhat, but it still leaves room for interpretation.)

Similarly, the concept of null is not present in XML. On the other hand, the concept of empty element does not map directly to object model. And they are not the same, empty element still can have attributes and it has a “type” (element name).

Generally, I think good solution is to have a special XML element representing null (<Null />), but another solution might make more sense in your specific case.

0

精彩评论

暂无评论...
验证码 换一张
取 消