Have you tried to [XmlIgnore] the actual DateTime member, and serialize a string rep instead? This is what a lot of us do anyway, in order to control the formatting... I believe null string fields aren't serialized, so it'd be good for that too.
http://www.windojitsu.com/blog/stillmoreonxmlserializer.html (The topic of that blog entry has nothing todo with DateTime formatting, but if you look at the DateTime member in the code snippet, you'll see an example of what I mean.) I vaguely recall first seeing this trick on Clemens Vasters' blog, but I can never seem to find it when I need it, so I snuck it into my piece on XmlElement vs IEnumerable. :) -S On Wed, 31 Mar 2004 16:51:06 -0800, Clark, Michael (OFM) <[EMAIL PROTECTED]> wrote: >I'm trying to control serialization in a class that contains a couple ints, >some strings, and a date. At times, each of these elements may be left >uninitialized. When serialized to xml, the uninitialized ints and strings >are simply not serialized. For example, using the following class fragment, >if Agency is left blank but the int and the date have values, the class is >serialized without it: ><Class GroupID="1" RefDate="1/1/2004 14:54:33" /> > >However, if the date is not initialized, it shows up anyway, as in: ><Class Agency="WSOC" GroupID="1" RefDate="0001-01-01T00:00:00.0000000- 08:00" >/> > >We'd really rather the RefDate attribute not show up at all if the date is >uninitialized, but I cannot find any options to accomplish this. > >Anyone know how to do this? > >Mike > >The class: > >[XmlAttribute] >public string Agency >{ > get{return _agency;} > set{_agency = value;} >} > >[XmlAttribute] >public int GroupID >{ > get{return _groupID;} > set{_groupID = value;} >} > >[XmlAttribute] >public DateTime RefDate >{ > get{return _refDate;} > set{_refDate = value;} >} > >=================================== >This list is hosted by DevelopMentor� http://www.develop.com >Some .NET courses you may be interested in: > >NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles >http://www.develop.com/courses/gaspdotnetls > >View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor� http://www.develop.com Some .NET courses you may be interested in: NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles http://www.develop.com/courses/gaspdotnetls View archives and manage your subscription(s) at http://discuss.develop.com
