Reputation: 3455
I have a date saved in xml and I am trying to parse that date then get the difference from DateTime.Now but I keep getting a format error when I try to parse the DateTime out from xml, am I formatting it right?
here is my xml
<Item>
<Name>John</Name>
<Date>5/4/2012</Date>
and my code
public void showItems()
{
using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = file.OpenFile("Item.xml", FileMode.Open))
{
XElement xml = XElement.Load(stream);
listBoxItem.ItemsSource = from item in xml.Descendants("Item")
select new Ages
{
Name = item.Element("Name").Value,
DOB = item.Element("Date").Value,
DIFF = DateTime.ParseExact(item.Element("Date").Value, "m d yyyy", CultureInfo.InvariantCulture)
};
}
}}
and my class
public class Ages
{
public string Name { get; set; }
public string DOB { get; set; }
public DateTime DIFF { get; set; }
}
I would appreciate if you could help.
Upvotes: 0
Views: 816
Reputation: 13350
EDIT: Use this code to get the date parsed correctly:
DateTime.ParseExact(item.Element("Date").Value, "M/d/yyyy", CultureInfo.InvariantCulture)
Upvotes: 1