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!