List< DateTime>的XML序列化.使用自定义的日期时间格式 [英] XML Serialization of a List<DateTime> using a custom datetime format

查看:91
本文介绍了List< DateTime>的XML序列化.使用自定义的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XML序列化有一些麻烦. (可能类似于此问题,但要考虑序列化而不是反序列化). 我需要使用自定义的DateTime格式序列化List<DateTime>. 我可以对单个DateTime变量执行此操作,但是我不知道如何对列表进行操作.

I have some troubles with an XML Serialization. (Probably something similar to THIS SO QUESTION, but regarding serialization instead of deserialization). I need to serialize a List<DateTime>, using a custom DateTime format. I'm able to do it for a single DateTime variable, but I can't figure out how to do it for a list.

这就是我要在代码中尝试做的事情.

Here is what I'm trying to do in my code.

对于简单的DateTime变量,我使用以下代码:

For the simple DateTime variable I use this code:

[XmlType("Time")]
public class Time : BaseClass
{
    public Time()
    {
        base.Name = string.Empty;
        StartTimeDt = DateTime.Now;            
    }

    [XmlIgnore]
    public DateTime StartTimeDt
    {
        get { return DateTime.Parse(StartTime); }
        set { StartTime = value.ToString("yyyy-MM-dd HH:mm:ss.fff"); }
    }

    [XmlAttribute("StartTime")]
    public string StartTime { get; set; }
}

获取此XML结构:

<Time Name="" StartTime="2014-05-05 11:00:00.000">

当我创建Time类的新实例并对其进行序列化时,我得到了我期望的XML结构,并且我的StartTime变量以自定义日期时间格式进行了序列化.

When I create a new instance of Time class and I serialize it, I obtain exatcly the XML structure I expect, with my StartTime variable serialized with the custom datetime format.

当我有List<DateTime>时,我尝试使用这种代码:

When I have a List<DateTime>, I try to use this kind of code:

[XmlType("Calendar")]
public class Calendar : BaseClass
{
    public Calendar()
    {
        base.Name = string.Empty;
        DaysDt = new List<DateTime>();
    }

    [XmlIgnore]
    public List<DateTime> DaysDt
    {
        get { return Days.Select(item => DateTime.Parse(item)).ToList(); }
        set { Days = value.Select(item => item.ToString("yyyy-MM-dd")).ToList(); }
    }

    [XmlArrayItem("Day", typeof(string))]
    public List<string> Days { get; set; }
}

生成此XML输出:

<Calendar Name="">
    <Days>
        <Day>2014-03-02</Day>
        <Day>2014-05-03</Day>
        <Day>[…]</Day>
    </Days>
</Annual>

但是我做的事情显然是错误的,因为当我创建Calendar类的新实例并尝试添加一些日期时间时

But I'm doing something obviously wrong, because when I create a new instance of Calendar class and I try to add some datetimes

Calendar calendar = new Calendar();
calendar.DaysDt = new List<DateTime>();
calendar.DaysDt.Add(DateTime.Now);
calendar.DaysDt.Add(DateTime.Now.AddDays(5));
calendar.DaysDt.Add(DateTime.Now.AddDays(10));

我的DaysDtDays列表始终包含0个对象,当我进行序列化时,它不序列化任何东西...

both my DaysDt and Days lists always contain 0 object, and when I go for serialization it serializes nothing...

此示例代码中是否有任何明显的错误?我很确定我在使用自定义格式的List<DateTime>List<string>之间的转换中犯了一些错误...

Any noticeable error in this sample code? I'm quite sure I'm making some errors in the transition between the List<DateTime> and the List<string> with the custom format...

推荐答案

您发布的代码有误.

没有任何序列化是合理的. DaysDt对象未序列化(因为设置了XmlIgnore属性),并且您从不为Days对象分配任何值.我想您可能在将项目添加到列表中时会混淆/设置.

It is reasonable that nothing serializes. The DaysDt object is not serialized (because you set the XmlIgnore attribute) and you never assign any values on the Days object. I presume you may have mixed up getting/setting with adding items to a list.

执行时:

calendar.DaysDt.Add(DateTime.Now);

不是叫塞特犬! 因此,您将获得一个空的Days对象.

The setter is not called! So that leaves you with an empty Days object.

以下面的代码为例,看看它是否有效:

Try this code for example and see if it works:

Calendar calendar = new Calendar();
List<DateTime> lst = new List<DateTime>();
lst.Add(DateTime.Now);
lst.Add(DateTime.Now.AddDays(5));
lst.Add(DateTime.Now.AddDays(10));
calendar.DaysDt = lst;

这篇关于List&lt; DateTime&gt;的XML序列化.使用自定义的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆