Question

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.

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; }
}

to obtain this XML structure:

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

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.

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; }
}

to generate this XML output:

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

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));

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

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...

Was it helpful?

Solution

There is a mistake on the code you posted.

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.

When you execute:

calendar.DaysDt.Add(DateTime.Now);

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top