title: Override XML Serialization Method
body: <p>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. </p>
  
  <pre><code>    [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&lt;MMU.Calendar.gCalEvent&gt; items = new Collection&lt;MMU.Calendar.gCalEvent&gt;();
  
  /*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;
  </code></pre>
  
  <p>I have tried to look this info up but there seems to be a lot about custom serializing to files but not to XML...</p>