【问题标题】:Why is System.TimeSpan not XML serializable?为什么 System.TimeSpan 不是 XML 可序列化的?
【发布时间】:2012-12-05 22:17:16
【问题描述】:

当我尝试将 TimeSpan 值存储到 ApplicationData 设置中时,我收到一个错误,因为 TimeSpan 无法序列化。这个 SO 问题显示了如何处理它:How to serialize a TimeSpan to XML

为什么 TimeSpan 不容易序列化?

还有,有不能序列化的数据类型列表吗?

【问题讨论】:

  • [Serializable]。使这成为一个糟糕的标题。
  • 不争,但TimeSpan为什么不序列化?
  • 它将序列化。只需使用 SoapFormatter 或 BinaryFormatter。
  • 我不认为这是重复的。另一个问题询问如何进行序列化,而这个问题询问为什么它不能正常工作。

标签: c# serialization xml-serialization timespan


【解决方案1】:

它不会序列化的原因是XmlCustomFormatter没有实现序列化的功能。

来自XmlCustomFormatter 类,这里是支持的可序列化数据列表(来自FromDefaultValue 方法):

DateTime

Date

Time

如果您查看源参考中的 XmlSerializationWriter 类,您可以看到它是如何用于序列化所有内容的:

请参阅WriteTypedPrimitive 方法以获取有关 primites 等的更多详细信息。

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.WriteTypedPrimitive"]/*' /> 
    protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType) {

关于对象和 xml 序列化的详细信息:

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromByteArrayBase64"]/*' /> 
    protected static byte[] FromByteArrayBase64(byte[] value) {
        // Unlike other "From" functions that one is just a place holder for automatic code generation. 
        // The reason is performance and memory consumption for (potentially) big 64base-encoded chunks
        // And it is assumed that the caller generates the code that will distinguish between byte[] and string return types
        //
        return value; 
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromByteArrayHex"]/*' /> 
    protected static string FromByteArrayHex(byte[] value) {
        return XmlCustomFormatter.FromByteArrayHex(value); 
    } 

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromDateTime"]/*' /> 
    protected static string FromDateTime(DateTime value) {
        return XmlCustomFormatter.FromDateTime(value);
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromDate"]/*' />
    protected static string FromDate(DateTime value) { 
        return XmlCustomFormatter.FromDate(value); 
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromTime"]/*' />
    protected static string FromTime(DateTime value) {
        return XmlCustomFormatter.FromTime(value);
    } 

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromChar"]/*' /> 
    protected static string FromChar(char value) { 
        return XmlCustomFormatter.FromChar(value);
    } 

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromEnum"]/*' />
    protected static string FromEnum(long value, string[] values, long[] ids) {
        return XmlCustomFormatter.FromEnum(value, values, ids, null); 
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromEnum1"]/*' /> 
    protected static string FromEnum(long value, string[] values, long[] ids, string typeName) {
        return XmlCustomFormatter.FromEnum(value, values, ids, typeName); 
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromXmlName"]/*' />
    protected static string FromXmlName(string name) { 
        return XmlCustomFormatter.FromXmlName(name);
    } 

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromXmlNCName"]/*' />
    protected static string FromXmlNCName(string ncName) { 
        return XmlCustomFormatter.FromXmlNCName(ncName);
    }

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromXmlNmToken"]/*' /> 
    protected static string FromXmlNmToken(string nmToken) {
        return XmlCustomFormatter.FromXmlNmToken(nmToken); 
    } 

    /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.FromXmlNmTokens"]/*' /> 
    protected static string FromXmlNmTokens(string nmTokens) {
        return XmlCustomFormatter.FromXmlNmTokens(nmTokens);
    }

【讨论】:

  • 如果我错了,请纠正我,但没有名为 Date 或 Time 的类?
  • @Petoj 你是对的。这三个人都在使用DataTime
猜你喜欢
  • 2014-03-29
  • 2011-01-15
  • 2018-03-05
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多