我是一名編程學生,我想知道當我在xml文件中序列化它時是否可以更改日期的格式。此日期是對象「Loan」的ObservableCollection的屬性,此對象具有兩個DateTime屬性,其中一個日期是可爲空的對象。我序列化包括日期在內的所有集合。在C#中將XML的DateTime屬性序列化爲XML
我想獲得在xml文件:
<OutDate> 15-03-2014 </OutDate>
<!--If the date is null I don´t want to appear the node-->
而且I'm得到這個:
<OutDate>2014-03-15T00:00:00</OutDate>
<InDate xsi:nil="true" />
這是我的代碼項目的一部分:我類貸款 部分,已標記爲可序列化,如下所示:
private string isbn;
private string dni;
private DateTime dateOut;
private DateTime? dateIn;
// Setters and Gettters and constructors
這是序列化:
// I will pass three collections to this method loans, books and 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);
}
}
召喚:
SerializeToXML<Loan>(_file, "Library", manager.LoansCollection);
Thnks。