0

I have field public TimeSpan TimeSpanField in myClass. I create instance of the myClass and fill field. Next I want to serialize it to XML and deserialize back to the object. I Know that Microsoft have problems with TimeSpan serialization therefore I found the answer How to serialize a TimeSpan to XML and use it. ok! It works good. But How to make similar for public TimeSpan[] TimeSpanArrayField. the indexer(http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx) in this case didn't help.

Code forpublic TimeSpan TimeSpanField

    [XmlIgnore]
    public TimeSpan TimeSpanField;

    [Browsable(false)]
    [XmlElement(DataType = "duration", ElementName = "TimeSpanField")]
    public string TimeSpanFieldString
    {
        get
        {
            return XmlConvert.ToString(TimeSpanField);
        }
        set
        {
            TimeSpanField = string.IsNullOrEmpty(value) ?
                TimeSpan.Zero : XmlConvert.ToTimeSpan(value);
        }
    }
Community
  • 1
  • 1
Kuchur Andrei
  • 117
  • 11
  • Convert it's value into something what you can serialize. – Sinatr Oct 02 '14 at 14:40
  • you could always just serialize a timespan to a long[] where it stores the ticks. You can then convert it back to a TimeSpan with the ticks constructor. – CathalMF Oct 02 '14 at 14:54
  • @CathalMF, I convert TimeSpan to string and back, it works good. i want to make similar for Timespan array – Kuchur Andrei Oct 02 '14 at 15:03

1 Answers1

0

Solved.

    [XmlIgnore]
    public TimeSpan[] TimeSpanArrayField;

    [Browsable(false)]
    [XmlElement(DataType = "duration", ElementName = "TimeSpanField")]
    public string[] TimeSpanFieldString
    {
        get
        {
            string[] strings = new string[TimeSpanArrayField.Length];
            for (int number = 1; number <= TimeSpanArrayField.Length; number++)
                strings[number - 1] = TimeSpanArrayField[number - 1].ToString();
            return strings;
        }
        set
        {
            TimeSpanArrayField = new TimeSpan[value.Length];
            for (int number = 1; number <= value.Length; number++)
                TimeSpanArrayField[number - 1] = TimeSpan.Parse(value[number - 1]);
        }
    }
Kuchur Andrei
  • 117
  • 11