Search code examples
c#.netxml-serializationxmlserializer

XMLSerializer serialising date error : Is not a valid AllXsd


Im using the XmlSerializer to serialise objects which contain a datetime property.

This serialises fine with the following an example of the results produced:

<GuestUserLinkItemActivity>
<ActivityDateTime>2013-03-06T00:00:00+00:00</ActivityDateTime>
<ActivityMessage>Invitation email sent to guest.</ActivityMessage>
</GuestUserLinkItemActivity>

But then when I try and deserialise, again using the XmlSerializer, this I get the following error

The string '2013-03-06T00:00:00 00:00' is not a valid AllXsd value. xmlserializer Deserialize

There are a couple of posts I have found with similar issues but non provide a solution to this.

XmlSerializer: The string '' is not a valid AllXsd value

The string '3/18/09 10:16 PM' is not a valid AllXsd value

It most likely to do with the date being stored in format not expected and from one of the articles not in the format expected by the xml specification. so I expect if I manually amend the date stored I could get it to deserialise it properly.

How can I get the dates stored properly using the XmlSerializer.

See bits of copy and pasted code below:

public class GuestUserLinkItemActivity
{
public DateTime ActivityDateTime { get; set; }
    public string ActivityMessage { get; set; }
}

Serializer class:

public static class Serializer
{

    public static T DeSerialise<T>(string contentsToDesrialise)
    {
        if (string.IsNullOrEmpty(contentsToDesrialise))
            return default(T);

        var xmlSer = new XmlSerializer(typeof(T));
        var stream = new MemoryStream();
        var sw = new StreamWriter(stream);
        sw.Write(contentsToDesrialise);
        sw.Flush();
        stream.Position = 0;
        var obj = (T)xmlSer.Deserialize(stream);
        sw.Close();
        stream.Close();
        return obj;
    }

    public static string Serialise<T>(T obectToSerialise)
    {
        var ms = new MemoryStream();
        var sr = new XmlSerializer(typeof(T));
        sr.Serialize(ms, obectToSerialise);
        ms.Position = 0;
        var sread = new StreamReader(ms);
        var serialisedObjectString = sread.ReadToEnd();
        sread.Close();
        ms.Close();
        return serialisedObjectString;
    }
}

Usage:

var guestHistoryList = new List<GuestUserLinkItemActivity>();
guestHistoryList.Add(new GuestUserLinkItemActivity(){
                         ActivityDateTime = DateTime.Now, 
                         ActivityMessage = "Invitation email sent to guest."}
);

var serialisedArray = Serializer.Serialise<GuestUserLinkItemActivity[]>(guestHistoryList.ToArray());
var deserialisedObject = Serializer.DeSerialise<GuestUserLinkItemActivity[]>(serialisedArray);

Solution

  • Instead of trying to serialize/deserialize an array, try with List<GuestUserLinkItemActivity> - it worked for me:

    var serialisedArray = Serializer.Serialise<List<GuestUserLinkItemActivity>>(guestHistoryList);
    var deserialisedObject1 = Serializer.DeSerialise<List<GuestUserLinkItemActivity>>(serialisedArray);