简体   繁体   English

TimeSpan [] XmlSerialization

[英]TimeSpan[] XmlSerialization

I have field public TimeSpan TimeSpanField in myClass. 我在myClass中有public TimeSpan TimeSpanField字段。 I create instance of the myClass and fill field. 我创建myClass的实例并填充字段。 Next I want to serialize it to XML and deserialize back to the object. 接下来,我想将其序列化为XML并反序列化回该对象。 I Know that Microsoft have problems with TimeSpan serialization therefore I found the answer How to serialize a TimeSpan to XML and use it. 我知道Microsoft在TimeSpan序列化方面遇到问题,因此我找到了如何将TimeSpan序列化为XML并使用它的答案。 ok! 好! It works good. 效果很好。 But How to make similar for public TimeSpan[] TimeSpanArrayField . 但是如何使public TimeSpan[] TimeSpanArrayField类似。 the indexer( http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx ) in this case didn't help. 在这种情况下,索引器( http://msdn.microsoft.com/zh-cn/library/6x16t2tx.aspx )并没有帮助。

Code for public TimeSpan TimeSpanField public 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);
        }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM