views:

1431

answers:

2
[XmlElement(ElementName = ElementConstants.CreateDate, 
    Namespace = "http://api.facebook.com/1.0/",
    DataType = "date", Type = typeof(DateTime))]
public DateTime CreateDate { get; set; }

And if I try taking out the DataType in the attribute I get : {"The string '1233469624' is not a valid AllXsd value."}

Here is an example of one of the node values:

<created>1230437805</created>

I'm not sure how to setup the DateTime property here for this to be successfully deserialized.

+10  A: 

I've already answered this for you a few days ago.

In your class, you need to do the translation:

static readonly DateTime epoch = new DateTime(1970, 1, 1);
static long SerializeDateTime(DateTime value)
{
    return (long)((value - epoch).TotalSeconds);
}
static DateTime DeserializeDateTime(long value)
{
    return epoch.AddSeconds(value);
}

[XmlIgnore]
public DateTime CreateDate { get; set; }

[XmlElement("created"), Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public long CreateDateInt64 {
    get {return SerializeDateTime(CreateDate);}
    set {CreateDate = DeserializeDateTime(value);}
}

etc. The CreateDateInt64 is the version that XmlSerializer uses - but when invoked, it does the translation and passes the value through to CreateDate (which is ignored by XmlSerializer due to the attribute).

This means you have a DateTime CreateDate on the class, and a number in the xml.

Marc Gravell
I was looking it up (he asked the same question 4 times, which didn't make it easy). And the questioner should already know I've given this linke!! I just wanted to prevent people spending their time looking too deep at something that has already been addressed.
Marc Gravell
It's cool :) upvote
Joel Coehoorn
Thanks for this answer. Had the same issue and was able to resolve using the method above.
DennyDotNet
+2  A: 

That looks like a unix-formatted timestamp to me. The Xml "date" datatype is very explicit in what the format should be:

yyyy-MM-ddTHH:mm:ss.fffffff+timezone offset

For example: 2009-02-09T10:01:23.3212345+06:00

Joel Coehoorn
The OP can't control the data coming back from facebook... it is more a translation exercise. But yes, it is a unix-epoch timestamp.
Marc Gravell
Wouldn't xml have a `T` in the middle of that?
Marc Gravell
Yes, it should. fixed.
Joel Coehoorn