XML 通过 c# 不接受日期时间值
Datetime value not accepted in XML through c#
无法将 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);
输出是:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
</AutoCount>
而不是:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
<CreatedDateTime>2015-05-03 18:01:35</CreatedDateTime>
</AutoCount>
当 xsd.exe 为可选元素(例如带有 minOccurs="0"
的元素)生成 class 定义时,其类型映射到值类型,例如 DateTime
,会生成一个额外的属性来表示它的值是否应该被序列化。
在这种情况下 CreatedDateTime
似乎是可选的,因此相关的 CreatedDateTimeSpecified
属性 应该设置为 true
:
data.CreatedDateTime = DateTime.Now;
data.CreatedDateTimeSpecified = true;
无法将 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);
输出是:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
</AutoCount>
而不是:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
<CreatedDateTime>2015-05-03 18:01:35</CreatedDateTime>
</AutoCount>
当 xsd.exe 为可选元素(例如带有 minOccurs="0"
的元素)生成 class 定义时,其类型映射到值类型,例如 DateTime
,会生成一个额外的属性来表示它的值是否应该被序列化。
在这种情况下 CreatedDateTime
似乎是可选的,因此相关的 CreatedDateTimeSpecified
属性 应该设置为 true
:
data.CreatedDateTime = DateTime.Now;
data.CreatedDateTimeSpecified = true;