I'm having trouble trying to customize the way DateTime variables are serialized in my objects. I want it to be output as 2011-09-26T13:00:00Z but when I override the GetObjectData() function as I believe is the way to do this, no XML data is output for them at all.
[DataContract(Namespace = "")]
[XmlRootAttribute(Namespace = "http://www.w3.org/2005/Atom", ElementName = "feed")]
public class GCal
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces _xsns = new XmlSerializerNamespaces();
[XmlElement(ElementName = "entry")]
public Collection<MMU.Calendar.gCalEvent> items = new Collection<MMU.Calendar.gCalEvent>();
/*some other elements*/
}
public class gCalEvent
{
[XmlElement(Namespace = "http://schemas.google.com/g/2005")]
public gdEvent when = new gdEvent();
/*some other elements*/
}
public class gdEvent : ISerializable
{
[XmlAttribute(AttributeName = "startTime")]
private DateTime _startTime;
[XmlAttribute(AttributeName = "endTime")]
private DateTime _endTime;
public gdEvent(DateTime startTime, DateTime endTime)
{
_startTime = startTime;
_endTime = endTime;
}
public gdEvent()
{
_startTime = DateTime.MinValue;
_endTime = DateTime.MinValue;
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
//needs to be in the format 2011-09-26T13:00:00Z
//if (_startTime != DateTime.MinValue)
info.AddValue("startTime", _startTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
//if (_endTime != DateTime.MinValue)
info.AddValue("endTime", _endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
}
}
GCal calendar = new GCal();
calendar = readSwsSpreadsheet(urlToCall);
stream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(typeof(GCal));
serializer.Serialize(stream, calendar);
stream.Seek(0, SeekOrigin.Begin);
Stream results = new MemoryStream();
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
return stream;
I have tried to look this info up but there seems to be a lot about custom serializing to files but not to XML...