[英]Parsing date from xml
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? 我有一个保存在xml中的日期,我试图解析该日期,然后从DateTime.Now中获取差值。现在,当我尝试从xml中解析出DateTime时,我一直收到格式错误,我对它进行格式化吗?
here is my xml 这是我的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. 如果您能提供帮助,我将不胜感激。
编辑:使用此代码来获取正确解析的日期:
DateTime.ParseExact(item.Element("Date").Value, "M/d/yyyy", CultureInfo.InvariantCulture)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.