linq查询WP7中的格式化日期

时间:2012-05-30 12:13:15

标签: xml linq windows-phone-7

我有一个linq查询显示在下面

    XDocument data = XDocument.Parse(xml);

            var persons = from query in data.Descendants("Table")
                          select new MailList
                          {
                              Sender = (string)query.Element("FromUser"),
                              Body = (string)query.Element("Message"),

                              Date = (string)query.Element("mDate"),
                              Time = (string)query.Element("mTime"),

                          };
            EmailList.ItemsSource = persons;

我想以“MM / yy”格式化日期,将时间格式化为“hh:mm” 谢谢

1 个答案:

答案 0 :(得分:0)

您不能使用DateTime的直接投射,因为它是值类型。使用普通演员表,但如果日期格式不正确,请注意FormatException

var persons = from query in data.Descendants("Table")
          select new MailList
          {
              Sender = (string)query.Element("FromUser"),
              Body = (string)query.Element("Message"),
              Date = ((DateTime)query.Element("mDate")).ToString("MM/yy"),
              Time = ((DateTime)query.Element("mTime")).ToString("hh:mm"),

          };