我是一名编程学生,想知道在将日期序列化为XML文件时是否可以更改其格式。此日期是“贷款”对象的ObservableCollection属性的属性,此对象具有两个DateTime属性,其中一个日期是可空对象。我将整个集合(包括日期)进行序列化。
我希望在XML文件中获得以下格式:
<OutDate> 15-03-2014 </OutDate>
<!--If the date is null I don´t want to appear the node-->
我看到的是这个:
并且我得到了这个:
<OutDate>2014-03-15T00:00:00</OutDate>
<InDate xsi:nil="true" />
这是我的代码项目的一部分: 我的类Loan的一部分已标记为可序列化,如下所示:
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);
谢谢。