Let's say I have the following XML:
<Account>
<AccountExpirationDate>6/1/2009</AccountExpirationDate>
</Account>
I want to use LINQ to XML to parse this into an object I'll call Account:
public class Account {
public DateTime? AccountExpirationDate { get; set; }
}
This is the C# code I've tried, but it won't let me use null:
var accountSettings =
from settings in templateXML.Descendants("Account")
select new Account {
AccountExpirationDate =
string.IsNullOrEmpty(settings.Element("AccountExpirationDate").Value)
? DateTime.Parse(settings.Element("AccountExpirationDate").Value)
: null
};
Is there a way for me to only assign AccountExpiration a date if the element exists in the XML? In my business logic it is acceptable for the value to be null. Thanks!