I have a simple XML document that looks like this:
<Person>
<LastName>LastName1</LastName>
<FirstName>FirstName1</FirstName>
<MiddleName>MiddleName1</MiddleName>
<Suffix>Suffix1</Suffix>
</Person>
However I have a constraint where I am not allowed to add empty tags. Therefore if the Suffix
value does not exist, I cannot use <Suffix />
or validation will fail.
I'm composing the XML structure using XElement
objects from different classes that return their respective XML via a returned XElement
object from a .ToXML()
method. I need to check per element to see if the XElement being returned is null
. If that's the case it has to be like that line never existed. I'm trying to use the ??
operator but I'm getting the error that ?? left operand is never null. I had the code as follows:
public XElement ToXML()
{
return new XElement("Employee",
new XElement(this.LastName.ToXML()) ?? null,
new XElement(this.FirstName.ToXML()) ?? null,
new XElement(this.MiddleName.ToXML()) ?? null,
new XElement(this.Suffix.ToXML()) ?? null);
}
How can I check per XML node to see if the XElement
object being returned is null and if so ignore adding/composing that node all together? Any help is appreciated, thanks!
A constructor in C# will either return a non-null reference to the object or will throw an exception. It will not return null*.
As for your problem, why not:
return new XElement("Employee",
this.LastName.ToXML(),
this.FirstName.ToXML(),
this.MiddleName.ToXML(),
this.Suffix.ToXML());
And just have each of those ToXML
methods return null
if none exist?
Or if your case is the properties themselves are null:
return new XElement("Employee",
this.LastName != null ? this.LastName.ToXML() : null, /* null is ignored */
this.FirstName != null ? this.FirstName.ToXML() : null,
this.MiddleName != null ? this.MiddleName.ToXML() : null,
this.Suffix != null ? this.Suffix.ToXML() : null);
I also just realized perhaps you always get back an XElement
, but it may be empty, in that case:
var elements = new[] { this.LastName.ToXML(), this.FirstName.ToXML(), ...
// use IsEmpty to filter out those that are <Element/>
return new XElement("Employee",
elements.Where(ee => ee != null && !ee.IsEmpty));
*I believe there is an interesting edge case where you could get this from a COM interface instantiation, but we'll ignore all "strange" coding.