c# - Override XML Serialization Method -


i'm having trouble trying customize way datetime variables serialized in objects. want output 2011-09-26t13:00:00z when override getobjectdata() function believe way this, no xml data output them @ 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 in 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 info there seems lot custom serializing files not xml...

you're trying customize standard serialization (iserializable) property type containing type being serialized xmlserializer. need implement ixmlserializable interface customize xml serialization. try this:

public class gdevent : ixmlserializable {     private datetime _starttime;     private datetime _endtime;      public gdevent(datetime starttime, datetime endtime)     {         _starttime = starttime;         _endtime = endtime;     }      public gdevent()     {         _starttime = datetime.minvalue;         _endtime = datetime.minvalue;     }      public xmlschema getschema()     {         return null;     }      public void writexml(xmlwriter writer)     {         if (_starttime != datetime.minvalue)             writer.writeattributestring("starttime", _starttime.tostring("yyyy-mm-ddthh:mm:ssz"));         if (_endtime != datetime.minvalue)             writer.writeattributestring("endtime", _endtime.tostring("yyyy-mm-ddthh:mm:ssz"));     }      public void readxml(xmlreader reader)     {         string starttimestring = reader.getattribute("starttime");         if (!string.isnullorempty(starttimestring))         {             _starttime = datetime.parse(starttimestring);         }         string endtimestring = reader.getattribute("starttime");         if (!string.isnullorempty(endtimestring))         {             _endtime = datetime.parse(endtimestring);         }     } 

}


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -