Instead of using the property Valuecast to string... and for int, instead int?. User-defined conversions to nullable types return null if source XAttribute/ XElementis null:
FinalDeck = (from deck in xmlDoc.Root.Element("Cards")
.Elements("Card")
select new CardDeck
{
Name = (string) deck.Attribute("name"),
Image = (string) deck.Element("Image").Attribute("path"),
Usage = (int?) deck.Element("Usage"),
Type = (string) deck.Element("Type"),
Strength = (int?) deck.Element("Ability") ?? 0
}).ToList();
, , Image , , path. , , , .
EDIT: :
public static XAttribute NullSafeAttribute(this XElement element, XName name)
{
return element == null ? null : element.Attribute(name);
}
:
Image = (string) deck.Element("Image").NullSafeAttribute("path"),