【发布时间】:2014-04-08 20:32:52
【问题描述】:
我是一名编程学生,我想知道当我在 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);
谢谢。
【问题讨论】:
标签: c# xml c#-4.0 datetime serialization