I have a model class
public class Item
{
public string Name {get; set;}
public string Desc {get; set;}
}
I would query my XML document
List<Item> item = xmlDoc.Descendants()
.Select(o => new Item {
Name = o.Attribute("name").Value,
Desc = o.Attribute("desc").Value
}).ToList();
However, the attribute desc may or may not be present for each item. The above LINQ works if the attribute desc is present, but will cause an exception if not.
If it does not exist I would like for the LINQ query to just assign null to the Desc field in the new Item object. Thank you for any suggestions.