M
McGiv
Hi,
I'm trying to serialise some objects and I've can't get the built in
serialisation to output exactly what I want. For the moment I'm
implementing the IXmlSerializable interface and doing it the long way.
For future reference is it possible to specify how a property should
be formatted when being serialised?
Example:
In the following code I want the Time property to be formatted not to
the default .ToString() but to .ToUniversalTime().ToString("r")
[Serializable]
public class Test
{
[XmlElement("time")]
public DateTime Time;
}
I was wondering is there an attribute that I can add along with
XmlElement to do this?
Perhaps something link:
[XmlElement("time"), XmlFormat(formatDate)]
public DateTime Time;
void FormatDate(object obj, XmlWriter writer)
{
DateTime dt = (DateTime)obj;
writer.WriteString( dt.ToUniversalTime().ToString("r") );
}
Where xmlformat created a delegate of type:
public delegate void XmlPropertyFormatter(object obj, XmlWriter
writer);
and when the Time property is to be serialised it calls the delegate
passing the Time property as the object parameter and the XmlWriter.
Cheers
Damien
I'm trying to serialise some objects and I've can't get the built in
serialisation to output exactly what I want. For the moment I'm
implementing the IXmlSerializable interface and doing it the long way.
For future reference is it possible to specify how a property should
be formatted when being serialised?
Example:
In the following code I want the Time property to be formatted not to
the default .ToString() but to .ToUniversalTime().ToString("r")
[Serializable]
public class Test
{
[XmlElement("time")]
public DateTime Time;
}
I was wondering is there an attribute that I can add along with
XmlElement to do this?
Perhaps something link:
[XmlElement("time"), XmlFormat(formatDate)]
public DateTime Time;
void FormatDate(object obj, XmlWriter writer)
{
DateTime dt = (DateTime)obj;
writer.WriteString( dt.ToUniversalTime().ToString("r") );
}
Where xmlformat created a delegate of type:
public delegate void XmlPropertyFormatter(object obj, XmlWriter
writer);
and when the Time property is to be serialised it calls the delegate
passing the Time property as the object parameter and the XmlWriter.
Cheers
Damien