我试图将. net TimeSpan对象序列化为XML,但它不起作用。快速谷歌表明,虽然TimeSpan是可序列化的,但XmlCustomFormatter没有提供将TimeSpan对象转换为XML或从XML转换的方法。

一种建议的方法是忽略TimeSpan进行序列化,而是序列化TimeSpan的结果。刻度(并使用新的TimeSpan(刻度)进行反序列化)。下面是一个例子:

[Serializable]
public class MyClass
{
    // Local Variable
    private TimeSpan m_TimeSinceLastEvent;

    // Public Property - XmlIgnore as it doesn't serialize anyway
    [XmlIgnore]
    public TimeSpan TimeSinceLastEvent
    {
        get { return m_TimeSinceLastEvent; }
        set { m_TimeSinceLastEvent = value; }
    }

    // Pretend property for serialization
    [XmlElement("TimeSinceLastEvent")]
    public long TimeSinceLastEventTicks
    {
        get { return m_TimeSinceLastEvent.Ticks; }
        set { m_TimeSinceLastEvent = new TimeSpan(value); }
    }
}

虽然这在我的简短测试中似乎是有效的,但这是实现这一目标的最佳方式吗?

是否有更好的方法将TimeSpan与XML进行序列化?


当前回答

如果不需要任何变通方法,请使用System.Runtime.Serialization.dll中的DataContractSerializer类。

        using (var fs = new FileStream("file.xml", FileMode.Create))
        {
            var serializer = new DataContractSerializer(typeof(List<SomeType>));
            serializer.WriteObject(fs, _items);
        }

其他回答

更可读的选项是将其序列化为字符串并使用TimeSpan。解析方法来反序列化它。你可以像你的例子中那样做,但是在getter中使用TimeSpan.ToString(),在setter中使用TimeSpan.Parse(value)。

对于数据契约序列化,我使用以下方法。

保持序列化属性为私有可以保持公共接口干净。 使用公共属性名进行序列化可以保持XML的整洁。

Public Property Duration As TimeSpan

<DataMember(Name:="Duration")>
Private Property DurationString As String
    Get
        Return Duration.ToString
    End Get
    Set(value As String)
        Duration = TimeSpan.Parse(value)
    End Set
End Property

Timespan以秒数的形式存储在xml中,但是我希望它很容易采用。 手动序列化的时间跨度(实现IXmlSerializable):

public class Settings : IXmlSerializable
{
    [XmlElement("IntervalInSeconds")]
    public TimeSpan Interval;

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("IntervalInSeconds", ((int)Interval.TotalSeconds).ToString());
    }

    public void ReadXml(XmlReader reader)
    {
        string element = null;
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
                element = reader.Name;
            else if (reader.NodeType == XmlNodeType.Text)
            {
                if (element == "IntervalInSeconds")
                    Interval = TimeSpan.FromSeconds(double.Parse(reader.Value.Replace(',', '.'), CultureInfo.InvariantCulture));
            }
       }
    }
}

还有一个更全面的例子: https://bitbucket.org/njkazakov/timespan-serialization

查看Settings.cs。 使用XmlElementAttribute需要一些复杂的代码。

我的解决方案。

[DataMember, XmlIgnore]
public TimeSpan MyTimeoutValue { get; set; }
[DataMember]
public string MyTimeout
{
    get { return MyTimeoutValue.ToString(); }
    set { MyTimeoutValue = TimeSpan.Parse(value); }
}

编辑:假设它是空的:

[DataMember, XmlIgnore]
public TimeSpan? MyTimeoutValue { get; set; }
[DataMember]
public string MyTimeout
{
    get 
    {
        if (MyTimeoutValue != null)
            return MyTimeoutValue.ToString();
        return null;
    }
    set 
    {
        TimeSpan outValue;
        if (TimeSpan.TryParse(value, out outValue))
            MyTimeoutValue = outValue;
        else
            MyTimeoutValue = null;
    }
}

如果不需要任何变通方法,请使用System.Runtime.Serialization.dll中的DataContractSerializer类。

        using (var fs = new FileStream("file.xml", FileMode.Create))
        {
            var serializer = new DataContractSerializer(typeof(List<SomeType>));
            serializer.WriteObject(fs, _items);
        }