[Mono-list] Xml Deserialize + Constructor Needed

A Rafael D Teixeira rafaelteixeirabr@hotmail.com
Tue, 14 Oct 2003 12:18:12 -0200


Well, it's a necessity for xml serialization (I learned it the hard way), 
that you have an "Default Constructor", what means that you need a 
parameterless constructor (besides your other constructors).

That is easy to understand why: when deserializing the xml, the deserializer 
sees an element that maps to a class, it then needs to create an instance of 
that class, an *empty* instance of the class, that later will be "fleshed" 
with other elements and attributes values (that's is also why only public 
fields/properties can be serialized, because the serializer/deserializer is 
outside the class).

If you furnish only constructors with parameters, how is the serializer 
going to find what values, should it be passing to your constructor?

Best regards,

Rafael Teixeira
Brazilian Polymath
Mono Hacker since 16 Jul 2001
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/



>From: Pablo Fischer <pablo@pablo.com.mx>
>To: mono-list@lists.ximian.com
>Subject: [Mono-list] Xml Deserialize + Constructor Needed
>Date: Mon, 13 Oct 2003 23:58:22 -0500
>
>Hi all!
>
>I created a file in XML with the help of XmlSerialization, how I want to
>Deserialize it, I found in may sites (also in msdn) a simple way to do
>this, so my method:
>
>private static void Desarmar() {
>  Accounts accs = new Accounts("test");
>  XmlSerialize mySerializer = new XmlSerializer(typeof(Accounts));
>  FileStream myFileStream = new FileStream(".mbloggyrc", FileMode.Open);
>  accs = (Accounts)mySerializer.Deserialize(myFileStream);
>}
>
>It compiles ok, but when I run it I get:
>
>Unhandled Exception: System.MissingMethodException: Default constructor
>not found
>in <0x00144> System.Activator:CreateInstance (System.Type,bool)
>in <0x00012> System.Activator:CreateInstance (System.Type)
>in <0x001ab>
>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadClassInstance 
>(System.Xml.Serialization.XmlTypeMapping,bool,bool)
>in <0x00067>
>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadObject
>(System.Xml.Serialization.XmlTypeMapping,bool,bool)
>in <0x000a2>
>System.Xml.Serialization.XmlSerializationReaderInterpreter:ReadObject ()
>in <0x00075> System.Xml.Serialization.XmlSerializer:Deserialize
>(System.Xml.Serialization.XmlSerializationReader)
>in <0x00068> System.Xml.Serialization.XmlSerializer:Deserialize
>(System.Xml.XmlReader)
>in <0x0003f> System.Xml.Serialization.XmlSerializer:Deserialize
>(System.IO.Stream)
>in <0x0009a> .testing:Desarmar ()
>in <0x0012c> .testing:Main ()
>
>A Default Constructor.. for what? I have a Constructor for each
>element..
>
>Thanks!
>Pablo
>--
>Pablo Fischer Sandoval (pablo [arroba/at] pablo.com.mx)
>http://www.pablo.com.mx
>http://www.debianmexico.org
>GPG FingerTip: 3D49 4CB8 8951 F2CA 8131 AF7C D1B9 1FB9 6B11 810C
>Firma URL: http://www.pablo.com.mx/firmagpg.txt
>
>_______________________________________________
>Mono-list maillist  -  Mono-list@lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-list

_________________________________________________________________
MSN Messenger: instale grátis e converse com seus amigos. 
http://messenger.msn.com.br