Serialize DateTime property to XML in C# -
i´m programming student , know if possible change format of date when serialize in xml file. date attribute of observablecollection of objects "loan", objects have 2 datetime properties, 1 of dates nullable object. serialize collection including date.
i obtain in xml file:
<outdate> 15-03-2014 </outdate> <!--if date null don´t want appear node-->
and i´m getting this:
<outdate>2014-03-15t00:00:00</outdate> <indate xsi:nil="true" />
this part of code project: part of class loan, mark serializable, looks this:
private string isbn; private string dni; private datetime dateout; private datetime? datein; // setters , gettters , constructors
this method serialize:
// pass 3 collections method loans, books , clients public void serializetoxml<t>(string file, string node, observablecollection<t> collection) { xmlrootattribute root = new xmlrootattribute(node); xmlserializer serializer = new xmlserializer(typeof(observablecollection<t>), root); using (filestream fs = new filestream(file, filemode.create)) { serializer.serialize(fs, collection); } }
the call:
serializetoxml<loan>(_file, "library", manager.loanscollection);
thnks.
probably simplest way achieve implement ixmlserializable interface on class instead. along following lines
public class loan : ixmlserializable { public void writexml(xmlwriter writer) { if(datein.hasvalue) { writer.writeelementstring("datein", datein.value.tostring()); } } }
on read you'll need read element name, if it's datein set that, otherwise set appropriate value. checking see if exists in xml.
Comments
Post a Comment