DateTime serialization in time without milliseconds and gmt

I created a C # class file using an XSD file as input. One of my properties looks like this:

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

When serialized, the contents of the file now look like this:

 <Time>14:04:02.1661975+02:00</Time> 

Is it possible, with the XmlAttributes in a property, to render it without milliseconds and GMT values ​​like this?

 <Time>14:04:02</Time> 

Is this possible, or do I need to hack some kind of xsl / xpath-replace-magic after the class has been serialized?

This is not a solution for changing an object to String, because it is used as a DateTime in the rest of the application and allows us to create an xml view from an object using the XmlSerializer.Serialize () method.

The reason I need to remove additional information from the field is because the receiving system does not meet the w3c standards for the time data type.

+11
time attributes xml-serialization
Sep 19 '08 at 12:44
source share
2 answers

You can create a string property that translates to / from the timeField field and place the serialization attribute on it, rather than the actual DateTime property that the rest of the application uses.

+13
Sep 19 '08 at 12:52
source share

Put [XmlIgnore] in the Time property.

Then add a new property:

 [XmlElement(DataType="string",ElementName="Time")] public String TimeString { get { return this.timeField.ToString("yyyy-MM-dd"); } set { this.timeField = DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture); } } 
+21
Sep 19 '08 at 13:04
source share



All Articles