Serializing a DataType=“time” field using XmlSerializer?

Take a look at this question stackoverflow.com/questions/101533/seria....

Wooo is that for real. Seems like a dirty hack to me.. There's no nicer way to do this? – CraftyFella Mar 8 '10 at 15:53 1 @CraftyFella, you can implement IXmlSerializable, but then you need to provide an implementation for all the serialization logic.

– João Angelo Mar 8 '10 at 15:56 1 as far as I know this is the only solution. When I have to do it I do it this with second property. If you find a better solution please let me know.

A solution that not involves writing your own XmlSerializer – IordanTanev Mar 8 '10 at 15:57.

I've had different issues with this myself... however I was attempting to serialize a TimeSpan object. The solution was to have two properties, one that held the TimeSpan, and one that was a string representation of the TimeSpan which got Serialized. Here was the pattern: XmlIgnore public TimeSpan ScheduledTime { get; set; } XmlElement("ScheduledTime", DataType="duration") public string XmlScheduledTime { get { return XmlConvert.

ToString(ScheduledTime); } set { ScheduledTime = XmlConvert. ToTimeSpan(value); } } However, with this code, the time is printed out in the following format: PT23H30M The W3C definition of duration is here which explains it.

Expanding on the comment I made on one of the others answers. Public class RecordExample : IXmlSerializable { public DateTime TheTime { get; set; } public DateTime TheDate { get; set; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { // TODO : Deserialization logic here } public void WriteXml(XmlWriter writer) { writer. WriteElementString( "date", this.TheDate.

ToString("yyyy-MM-dd")); writer. WriteElementString( "time", this.TheTime. ToString("hh:mm:ss.FK")); } } Serializing like this: var rc = new RecordExample() { TheDate = DateTime.

Today, TheTime = DateTime. UtcNow }; var serializer = new XmlSerializer(typeof(RecordExample)); var ms = new MemoryStream(); serializer. Serialize(ms, rc); ms.

Seek(0, SeekOrigin. Begin); Console. WriteLine(new StreamReader(ms).ReadToEnd()); Output example: 2010-03-08 04:26:16.1Z.

Thanks.. that's a definite option. Also didn't know about the UtcNow. – CraftyFella Mar 9 '10 at 9:53.

I concur with the other answers (I was not done writing when they popped up). It does not look like it is possible, in a direct way. A look at the source with Reflector shows that a time value ends up being converted to a string with the System.Xml.XmlConvert.

ToString, that has a hard-coded format of: HH:mm:ss. Fffffffzzzzzz So having two properties, the real one being XmlIgnore and a string that you build yourself is a good way to go.

Very thorough thanks for checking that out.. – CraftyFella Mar 9 '10 at 9:50.

I've had different issues with this myself... however I was attempting to serialize a TimeSpan object. The solution was to have two properties, one that held the TimeSpan, and one that was a string representation of the TimeSpan which got Serialized. Here was the pattern.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions