I would like to get the two attribute-assignment lines below into one line since I'm going to build them into an application where they will be numerous.
Is there a way to express those two lines in one line of elegantly constructed C#, perhaps with a ?? operator like this?
string nnn = xml.Element("lastName").Attribute("display").Value ?? "";
Here's the code:
using System;
using System.Xml.Linq;
namespace TestNoAttribute
{
class Program
{
static void Main(string[] args)
{
XElement xml = new XElement(
new XElement("employee",
new XAttribute("id", "23"),
new XElement("firstName", new XAttribute("display", "true"), "Jim"),
new XElement("lastName", "Smith")));
//is there any way to use ?? to combine this to one line?
XAttribute attribute = xml.Element("lastName").Attribute("display");
string lastNameDisplay = attribute == null ? "NONE" : attribute.Value;
Console.WriteLine(xml);
Console.WriteLine(lastNameDisplay);
Console.ReadLine();
}
}
}