I have two instances of XElement:
var el1 = new System.Xml.Linq.XElement("xel", null);
var el2 = new System.Xml.Linq.XElement("xel", string.Empty);
el1
looks like this:
<xel />
el2
looks like this:
<xel></xel>
Yet, the Value
property of both is equal to string.Empty
.
I can think of plenty of hacks to differentiate null
from string.Empty
in an XElement
, but is there something built into the framework to do this that I'm missing?
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all.
A string refers to a character's sequence. Sometimes strings can be empty or NULL. The difference is that NULL is used to refer to nothing. However, an empty string is used to point to a unique string with zero length.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.
Note that every string in Python takes additional 49-80 bytes of memory, where it stores supplementary information, such as hash, length, length in bytes, encoding type and string flags. That's why an empty string takes 49 bytes of memory.
el1.IsEmpty
will return true, on the other hand, el2.IsEmpty
will return false.
From the XML Schema Standard:
2.6.2 xsi:nil
XML Schema: Structures introduces a mechanism for signaling that an element should be accepted as ·valid· when it has no content despite a content type which does not require or even necessarily allow empty content. An element may be ·valid· without content if it has the attribute xsi:nil with the value true. An element so labeled must be empty, but can carry attributes if permitted by the corresponding complex type.
So for you, you'd have to add the xsi namespace into your XmlDocument. Then the element would look like
<xel xsi:nil="true" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With