Search code examples
c#.netlinq-to-xmlxelement

Differentiate null and string.Empty values in an 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 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?


Solution

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