[Mono-list] Problem with xml.Serialization

Sergio Paracuellos par@dragon-lance.net
Sun, 17 Oct 2004 18:48:46 +0200


--=-Y6dBQE+ObxBctjDcuGE7
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Thank you very much for the example.=20

Now, I solve de problem but I have had to solve a bug of mcs too.=20

http://bugzilla.ximian.com/show_bug.cgi?id=3D61358

Regards,

	Sergio

El vie, 15-10-2004 a las 11:10 +0200, RoBiK escribi=F3:
> Hi,
>=20
> I see another problem here. It is not an good idea to derive from ArrayLi=
st,
> because the XmlSerializer has special handling for ArrayList and also
> classes that derive from it. Instead you should Package the ArrayList ind=
o
> another class (this may be for example a custom collection type).
> The second thing you need to do is to tell the serializer if there are an=
y
> instances of custom objets, that you are inserting into the ArrayList. Th=
is
> is done by the XmlInclude attribute.
> Try this example:
>=20
> using System;
> using System.Collections;
> using System.IO;
> using System.Xml.Serialization;
> using System.Text;
> namespace XmlSerializationTest
> {
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             Subject subject =3D new Subject();
>             subject.name =3D "Subject Name";
>             subject.comment =3D "Subject Comment";
>             SubjectList subjectList =3D new SubjectList();
>             subjectList.subject =3D subject;
>             subjectList.list.Add("Test");
>             subjectList.list.Add(123);
>             CustomObject co =3D new CustomObject();
>             co.customString =3D "Custom String";
>             subjectList.list.Add(co);
>             XmlSerializer serializer =3D new
> XmlSerializer(typeof(SubjectList));
>             StringBuilder sb =3D new StringBuilder();
>             StringWriter sw =3D new StringWriter(sb);
>             serializer.Serialize(sw, subjectList);
>             Console.WriteLine(sb.ToString());
>             Console.ReadLine();
>         }
>     }
>     [XmlInclude(typeof(CustomObject))]
>     public class SubjectList
>     {
>         public Subject subject;
>         public ArrayList list;
>         public SubjectList()
>         {
>             list =3D new ArrayList();
>         }
>     }
>     public class Subject
>     {
>         public string name;
>         public string comment;
>     }
>     public class CustomObject
>     {
>         public string customString;
>     }
> }
>=20
> Robert
>=20
> -----Original Message-----
> From: Sergio Paracuellos [mailto:par@dragon-lance.net]=20
> Sent: Donnerstag, 14. Oktober 2004 16:03
> To: RoBiK
> Cc: mono-list@lists.ximian.com
> Subject: RE: [Mono-list] Problem with xml.Serialization
>=20
> El jue, 14-10-2004 a las 15:42 +0200, RoBiK escribi=F3:
> > As the error message says: The type gorganizer.Subject was not=20
> > expected. Use the XmlInclude or SoapInclude attribute to specify types=20
> > that are not known statically.
> > You are assinging an instance of type gorganizer.Subject to a "obj"=20
> > variable of type object. When the serializer tries to serialize this=20
> > variable, it finds that this is not an instance of type object but=20
> > something it did not expected. So either use the XmlInlude attribude=20
> > to tell the serializer what to expect, or rework your code to use anoth=
er
> aproach.
> >=20
> > Robert
>=20
> If I change the list to work with "Subject" the error is the same.
>=20
> Could you put me an example of how to say the serializer what I want to
> serialize?=20
>=20
> Thanks!
>=20
> >=20
> >=20
> >=20
> > -----Original Message-----
> > From: mono-list-admin@lists.ximian.com=20
> > [mailto:mono-list-admin@lists.ximian.com] On Behalf Of Sergio=20
> > Paracuellos
> > Sent: Donnerstag, 14. Oktober 2004 15:27
> > To: mono-list@lists.ximian.com
> > Subject: [Mono-list] Problem with xml.Serialization
> >=20
> > Hi!
> >=20
> > I'm doing an application and I need to serialize in xml a list of=20
> > objects. I get the following error:
> >=20
> > Unhandled Exception: System.InvalidOperationException: The type=20
> > gorganizer.Subject was not expected. Use the XmlInclude or SoapInclude=20
> > attribute to specify types that are not known statically.
> > in <0x001d0>
> > System.Xml.Serialization.XmlSerializationWriter:WriteTypedPrimitive
> > (string,string,object,bool)
> > in <0x00380>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> > (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,boo
> > l,bool
> > )
> > in <0x00ca0>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMember
> > Elemen t (System.Xml.Serialization.XmlTypeMapElementInfo,object)
> > in <0x004f8>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListCo
> > ntent=20
> > (System.Xml.Serialization.TypeData,System.Xml.Serialization.ListMap,ob
> > ject,S
> > ystem.Text.StringBuilder)
> > in <0x0024c>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListEl
> > ement
> > (System.Xml.Serialization.XmlTypeMapping,object,string,string)
> > in <0x0050c>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> > (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,boo
> > l,bool
> > )
> > in <0x001c0>
> > System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteRoot
> > (object)
> > in <0x000e0> System.Xml.Serialization.XmlSerializer:Serialize
> > (object,System.Xml.Serialization.XmlSerializationWriter)
> > in <0x00150> System.Xml.Serialization.XmlSerializer:Serialize
> > (System.Xml.XmlWriter,object,System.Xml.Serialization.XmlSerializerNam
> > espace
> > s)
> > in <0x000a4> System.Xml.Serialization.XmlSerializer:Serialize
> > (System.IO.Stream,object)
> > in <0x000b4> gorganizer.MainWindow:SaveDataToXml () in <0x0002c>=20
> > gorganizer.MainWindow:OnSaveClicked
> > (object,System.EventArgs)
> > in <0x000bc> (wrapper delegate-invoke)=20
> > System.MulticastDelegate:invoke_void_object_EventArgs
> > (object,System.EventArgs)
> > in <0x001c8> GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int)=20
> > in <0x00094> (wrapper native-to-managed)=20
> > GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int) in=20
> > (unmanaged) (wrapper managed-to-native) Gtk.Application:gtk_main () in=20
> > <0x00080> (wrapper managed-to-native) Gtk.Application:gtk_main () in=20
> > <0x00014> Gtk.Application:Run () in <0x00044>=20
> > gorganizer.Gorganizer:Main ()
> >=20
> > A resume of the code:
> >=20
> > When I push save button:
> >=20
> >  private void OnSaveClicked (object o, EventArgs args) {
> >      this.SaveDataToXml();
> >  }
> >=20
> >=20
> > private void SaveDataToXml () {
> >   using (FileStream fs =3D new FileStream("data.xml", FileMode.Create))=
 {
> >      XmlSerializer serializer =3D new XmlSerializer(typeof(SubjectList)=
);
> >      serializer.Serialize(fs, mySubjectList);
> >   }
> > }
> >=20
> > * SubjectList.cs:
> >=20
> > using System;
> > using System.Collections;
> > using System.Xml.Serialization;
> >=20
> > namespace gorganizer {
> >=20
> >     public class SubjectList: ArrayList, IMyList {
> >         private object obj;
> >         private int length;
> >=20
> >         //For Serialization
> >         public SubjectList (){}
> >=20
> >         public Subject Obj {
> >             get {
> >                 return ((Subject)obj);
> >             }
> >         }
> >  =09
> > 	public int Length () {
> >             return this.Count;
> >         }
> >=20
> >         /*
> >          * Add an object to list if doesn't exists
> >          */
> >         public bool AddToList(object obj) {
> >             if (obj !=3D null && !this.Contains(obj)) {
> >                 this.Add(obj);
> >                 Console.WriteLine("Insertado");
> >                 return true;
> >             }
> >             return false;
> >         }
> >=20
> >         /*
> >          * Remove a object if it is in the list
> >          */
> >         public bool RemoveFromList(object obj) {
> >             if (this.Contains(obj) && obj !=3D null) {
> >                 this.Remove(obj);
> >                 Console.WriteLine("Eliminado");
> >                 return true;
> >             }
> >             return false;
> >         }
> >=20
> >  public void ListElements (ArrayList myList) {
> >             this.PrintValues(myList);
> >         }
> >=20
> >         /* returns a Subject from specified name */
> >         public Subject getSubject (string name) {
> >             Subject aux =3D null;
> >             Subject s =3D null;
> >             IEnumerator i =3D this.GetEnumerator();
> >             while (i.MoveNext()) {
> >                 if (i.Current is Subject) {
> >                     aux =3D (Subject) i.Current;
> >                     if (aux.Name.Equals(name)) {
> >                         s =3D aux;
> >                     }
> >                 }
> >             }
> >             return s;
> >         }
> >=20
> >         private void PrintValues (ArrayList myList) {
> >             IEnumerator myEnumerator =3D myList.GetEnumerator();
> >             while (myEnumerator.MoveNext())
> >                 Console.WriteLine(myEnumerator.Current.ToString());
> >         }
> >     }
> > }
> >=20
> > Subject.cs:
> >=20
> > using System;
> > using System.Xml.Serialization;
> >=20
> > namespace gorganizer {
> >=20
> >     public class Subject {
> >=20
> >         //Constructor
> >         public Subject () {
> >         }
> >=20
> >         private string name;
> >         public string Name {
> >             get {
> >                 return name;
> >             }
> >             set {
> >                 name =3D value;
> >             }
> >         }
> >=20
> >         private TimeTable theory;
> >         public TimeTable Theory {
> >             get {
> >                 return theory;
> >             }
> >             set {
> >                 theory =3D value;
> >             }
> >         }
> >=20
> >         private string comment;
> >         public string Comment {
> >             get {
> >                 return comment;
> >             }
> >             set {
> >                 comment =3D value;
> >             }
> >         }
> >=20
> >         public void DeleteFields () {
> >             name =3D null;
> >             theory =3D null;
> >             comment =3D null;
> >         }
> >     }
> > }
> >=20
> > Any Idea of what it's happening?
> >=20
> > Thanks to all,
> >=20
> > Regards,
> >=20
> > 	Sergio Paracuellos
> >=20
> > _______________________________________________
> > Mono-list maillist  -  Mono-list@lists.ximian.com=20
> > http://lists.ximian.com/mailman/listinfo/mono-list
>=20
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

--=-Y6dBQE+ObxBctjDcuGE7
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada
	digitalmente

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQBBcqJui0GPOekbrtERAvoPAKD0g5XfZgi7IUQTWs1A1VxGjfZyEACfdQH9
JiiNtH66rkFfXnj9eBMuk8E=
=Gw9m
-----END PGP SIGNATURE-----

--=-Y6dBQE+ObxBctjDcuGE7--