Reputation: 33
Unable to pass datetime.now value to a node 'createddatetime'. Output xml file discards the node. i used the following code,
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);
And the out put was:
<?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>
Instead of :
<?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>
Upvotes: 1
Views: 143
Reputation: 26213
When a class definition is generated by xsd.exe for an optional element (one with minOccurs="0"
, for example) with a type that maps to a value type such as DateTime
, an additional property will be generated to indicate whether or not its value should be serialized.
In this case it would seem CreatedDateTime
is optional, so the related CreatedDateTimeSpecified
property should be set to true
:
data.CreatedDateTime = DateTime.Now;
data.CreatedDateTimeSpecified = true;
Upvotes: 1