Distinguish between null and string.Empty in XElement

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 as follows:

 <xel /> 

el2 as follows:

 <xel></xel> 

However, the Value property is equal to string.Empty .

I can come up with a lot of hacks to distinguish null from string.Empty from XElement , but is there something built into the framework to make it that I am missing?

+6
source share
2 answers

el1.IsEmpty will return true, on the other hand, el2.IsEmpty will return false.

+4
source

From the XML Schema Standard :

2.6.2 xsi: nil

XML Schema: Structures provide a mechanism for signaling that an element should be accepted as Β· valid Β· when it has no content, despite the type of content that does not require or even does not allow empty content. An element can be Β· valid Β· without content if it has the xsi: nil attribute with a value of true. An element marked in this way should be empty, but can carry attributes if allowed by the corresponding complex type.

So, for you, you need to add the xsi namespace to your XmlDocument. Then the item will look like

 <xel xsi:nil="true" /> 
+2
source

All Articles