通过c#不接受XML中的日期时间值

无法将datetime.now值传递给节点’createddatetime’。 输出xml文件丢弃该节点。 我使用以下代码,

string PATH = "C:\\Samplex.xml"; CreateEmptyFile(PATH); var data = new AutoCount(); data.Product = "AutoCount Accounting"; data.Version = "1.5"; data.CreatedApplication = "BApp"; data.CreatedBy = "Business Solutions"; data.CreatedDateTime = DateTime.Now; /* this line*/ var serializer = new XmlSerializer(typeof(AutoCount)); using (var stream = new StreamWriter(PATH)) serializer.Serialize(stream, data); 

而且输出是:

    AutoCount Accounting 1.5 BApp Business Solutions  

代替 :

    AutoCount Accounting 1.5 BApp Business Solutions 2015-05-03 18:01:35  

当xsd.exe为可选元素(例如,一个minOccurs="0" )生成类定义时,其类型映射到值类型(如DateTime ,将生成另一个属性以指示是否为它的值应该被序列化。

在这种情况下,似乎CreatedDateTime是可选的,因此相关的CreatedDateTimeSpecified属性应设置为true

 data.CreatedDateTime = DateTime.Now; data.CreatedDateTimeSpecified = true;