c# - How to serialize the object of type DateTimeFormatInfo? -


i want serialize object of type datetimeformatinfo.

i tried following code:

datetimeformatinfo datetimeformat = new datetimeformatinfo(); datetimeformat.shortdatepattern = "dd-mmm-yy"; xs = new xmlserializer(datetimeformat.gettype()); streamwriter sw = new streamwriter("setting.xml"); xs.serialize(sw, datetimeformat); 

but throws below exception.

system.invalidoperationexception unhandled.
there error generating xml document.
type system.globalization.gregoriancalendar not expected.
use xmlinclude or soapinclude attribute specify types not known statically.

is need add serializing datetimeformatinfo?

you need include in xmlserializer list of addictional object types serialize. in case, need add object type system.globalization.gregoriancalendar.

        system.globalization.datetimeformatinfo datetimeformat = new system.globalization.datetimeformatinfo();         datetimeformat.shortdatepattern = "dd-mmm-yy";          // add here types need serialize         type[] extratypes = new type[] { typeof(system.globalization.gregoriancalendar) };          system.xml.serialization.xmlserializer xs = new system.xml.serialization.xmlserializer(datetimeformat.gettype(), extratypes);         system.io.streamwriter sw = new system.io.streamwriter(@"c:\testso.xml");         xs.serialize(sw, datetimeformat); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -