8

When I try to store a TimeSpan value into ApplicationData Settings I get an error because TimeSpan cannot be serialized. This SO question shows how to handle it: How to serialize a TimeSpan to XML

Why is it that TimeSpan would not be easily serializable?

And, is there a list of data types that cannot be serialized?

Community
  • 1
  • 1
Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • 1
    It _is_ `[Serializable]`. Makes this a bad title. – H H Dec 18 '12 at 16:48
  • 1
    Not to argue, but why won't TimeSpan serialize? – Jerry Nixon Dec 18 '12 at 16:49
  • 1
    It will serialize. Just use a SoapFormatter or BinaryFormatter. – H H Dec 18 '12 at 16:51
  • 2
    possible duplicate of [.NET How to serialize a TimeSpan to XML](http://stackoverflow.com/questions/637933/net-how-to-serialize-a-timespan-to-xml) – Lloyd Dec 18 '12 at 16:56
  • It's marked as serializable, but the XMLCustomFormatter does not handle it. I think the question is why MS made that choice, to which the answer is, you'd have to ask MS. There's no mention of it in the SSCLI that I can find. – Pete Dec 18 '12 at 17:02
  • 2
    I don't think this is a duplicate. The other question asks how to serialize anyway and this one is asking why it doesn't just work. – N_A Dec 18 '12 at 17:06

1 Answers1

8

The reason it won't serialize is because XmlCustomFormatter doesn't implement the functionality to serialize it.

From the XmlCustomFormatter class, here is a list of supported serializable data (from the FromDefaultValue method):

DateTime

Date

Time

You can see how it is used to serialize everything if you look at the XmlSerializationWriter class in the source reference:

See the WriteTypedPrimitive method for more details on primites etc.

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

Details on object and xml serialization:

    /// <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);
    }
N_A
  • 19,799
  • 4
  • 52
  • 98