Can you specify format for XmlSerialization of a datetime?
No, there isn't. If it's in that format, then it's not a valid dateTime as far as XML Schema is concerned.
The best you can do is as follows:
[XmlIgnore]
public DateTime DoNotSerialize {get;set;}
public string ProxyDateTime {
get {return DoNotSerialize.ToString("yyyyMMdd");}
set {DoNotSerialize = DateTime.Parse(value);}
}
Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss'
In the past, I've done the following to control datetime serialization:
- Ignore the DateTime property.
- Create a dummy string property that serializes/deserializes the way I want
Here is an example:
public class SomeClass
{
[XmlIgnore]
public DateTime SomeDate { get; set; }
[XmlElement("SomeDate")]
public string SomeDateString
{
get { return this.SomeDate.ToString("yyyy-MM-dd HH:mm:ss"); }
set { this.SomeDate = DateTime.Parse(value); }
}
}
How to serialize Xml Date only from DateTime in C#
You should use the XmlElementAttribute.DataType
property and specify date
.
public class Birthdays
{
[XmlElement(DataType="date")]
public DateTime DateOfBirth {get;set;}
public string Name {get;set;}
}
Using this outputs
<?xml version="1.0" encoding="utf-16"?>
<Birthdays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DateOfBirth>2013-11-14</DateOfBirth>
<Name>John Smith</Name>
</Birthdays>
Another option is to use a string
property just for serialization (backed by a DateTime
property you use), as at Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss' (this is needed for DataContractSerializer
, where the xs:date
type is not as well-supported)
XML Serialization of a ListDateTime using a custom datetime format
There is a mistake on the code you posted.
It is reasonable that nothing serializes. The DaysDt object is not serialized (because you set the XmlIgnore attribute) and you never assign any values on the Days object. I presume you may have mixed up getting/setting with adding items to a list.
When you execute:
calendar.DaysDt.Add(DateTime.Now);
The setter is not called!
So that leaves you with an empty Days object.
Try this code for example and see if it works:
Calendar calendar = new Calendar();
List<DateTime> lst = new List<DateTime>();
lst.Add(DateTime.Now);
lst.Add(DateTime.Now.AddDays(5));
lst.Add(DateTime.Now.AddDays(10));
calendar.DaysDt = lst;
Serialize DateTime property to XML in C#
Probably the simplest way to achieve this is to implement the IXmlSerializable interface on your class instead. Something along the following lines
public class Loan : IXmlSerializable
{
public void WriteXml(XmlWriter writer)
{
if(dateIn.HasValue)
{
writer.WriteElementString("dateIn", dateIn.Value.ToString());
}
}
}
On the read you'll need to read the Element name, if it's dateIn set that, otherwise set the appropriate value. Basically checking to see if it exists in the XML.
Controlling DateTime format for XML responses in Web API
You shouldn't specify your own format - you should instead tell the XML serializer that the value is just meant to be a date, using:
[XmlElement(DataType="date")]
Then force the use of XML serializer with config.Formatters.XmlFormatter.UseXmlSerializer = true
and you should be fine. That way you'll be using the standard (ISO-8601) format for dates, which is yyyy-MM-dd
... which means standard tools (e.g. LINQ to XML) will be able to parse the XML appropriately.
Related Topics
Htmlagilitypack -- Does <Form> Close Itself for Some Reason
How to Embed an Application Manifest into an Application Using VS2008
Why Enums Require an Explicit Cast to Int Type
Getting a System.Type from Type's Partial Name
ASP.NET Urlencode Ampersand for Use in Query String
How to Pinvoke to Getwindowlongptr and Setwindowlongptr on 32-Bit Platforms
Reading Excel Files as a Server Process
C# Create/Modify/Read .Xlsx Files
Why Does C# Limit the Set of Types That Can Be Declared as Const
How to Use Switch-Case on a Type
Azure Asp .Net Webapp the Request Timed Out
Returning in the Middle of a Using Block
Xmlwriter to Write to a String Instead of to a File
Waiting for Async/Await Inside a Task
How to Hide "Chrome Is Being Controlled by Automated Software" Infobar Within Chrome V76
How to Merge a List of Lists with Same Type of Items to a Single List of Items