views:

100

answers:

2

Hello,

I have generated classes from xsd and want to serialize the DateTime. My class looks like

private System.DateTime timeGMT; 

 [System.Xml.Serialization.XmlElementAttribute(DataType="time")] 
 public System.DateTime TimeGMT { 
     get { 
         return this.timeGMT; 
     } 
     set { 
         this.timeGMT= value; 
     } 
 }

But when I assign any DateTime object It serializes in format as

<TimeGMT>12:00:00.0000000-04:00</TimeGMT>

But I want it to be serialized as

<TimeGMT>12:00:00</TimeGMT>

I had a look at this question :

http://stackoverflow.com/questions/101533/serializing-datetime-to-time-without-milliseconds-and-gmt

which is similar to my case. But my problem is I also want to validate the generated xml against xsd. So I cannot convert the return type to string. (If I use String as return type then get an exception while generating XML as

time is an invalid value for XMLElementAttribute.DataType property.The property may be specified for only primitive types.

)

Is there any other way out there? Thanks in advance.

A: 

The solution in your linked question is what you need to do. You can still validate the resulting XML against an XSD, but the XSD should not define this element as an XML datetime type (because that's the one with the milliseconds and timezone, which you said you don't want). Instead you will have to define your own XSD type that matches what you want.

Evgeny
A: 

I didn't change the XML schema because it is used as standard. I removed

[System.Xml.Serialization.XmlElementAttribute(DataType="time")]  

from my class and used the solution suggested in

http://stackoverflow.com/questions/101533/serializing-datetime-to-time-without-milliseconds-and-gmt

Archie