Main Page | Report this Page
.NET DotNet Forum Index  »  Visual C# Forum  »  Serialisation auto formatting
Page 1 of 1    

Serialisation auto formatting

Author Message
McGiv
Posted: Fri Oct 01, 2004 1:49 pm
Guest
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
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Thu Dec 10, 2009 3:03 am