[Mono-list] Problem with xml.Serialization

Sergio Paracuellos par@dragon-lance.net
Thu, 14 Oct 2004 16:02:51 +0200


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

El jue, 14-10-2004 a las 15:42 +0200, RoBiK escribi=F3:
> As the error message says: The type gorganizer.Subject was not expected. =
Use
> the XmlInclude or SoapInclude attribute to specify types that are not kno=
wn
> statically.
> You are assinging an instance of type gorganizer.Subject to a "obj" varia=
ble
> of type object. When the serializer tries to serialize this variable, it
> finds that this is not an instance of type object but something it did no=
t
> expected. So either use the XmlInlude attribude to tell the serializer wh=
at
> to expect, or rework your code to use another aproach.
>=20
> Robert

If I change the list to work with "Subject" the error is the same.

Could you put me an example of how to say the serializer what I want to
serialize?=20

Thanks!

>=20
>=20
>=20
> -----Original Message-----
> From: mono-list-admin@lists.ximian.com
> [mailto:mono-list-admin@lists.ximian.com] On Behalf Of Sergio 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 objects=
. I
> get the following error:
>=20
> Unhandled Exception: System.InvalidOperationException: The type
> gorganizer.Subject was not expected. Use the XmlInclude or SoapInclude
> 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,bool,b=
ool
> )
> in <0x00ca0>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberEle=
men
> t (System.Xml.Serialization.XmlTypeMapElementInfo,object)
> in <0x004f8>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListConte=
nt
> (System.Xml.Serialization.TypeData,System.Xml.Serialization.ListMap,objec=
t,S
> ystem.Text.StringBuilder)
> in <0x0024c>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListEleme=
nt
> (System.Xml.Serialization.XmlTypeMapping,object,string,string)
> in <0x0050c>
> System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
> (System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,b=
ool
> )
> 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.XmlSerializerNamesp=
ace
> s)
> in <0x000a4> System.Xml.Serialization.XmlSerializer:Serialize
> (System.IO.Stream,object)
> in <0x000b4> gorganizer.MainWindow:SaveDataToXml () in <0x0002c>
> gorganizer.MainWindow:OnSaveClicked
> (object,System.EventArgs)
> in <0x000bc> (wrapper delegate-invoke)
> System.MulticastDelegate:invoke_void_object_EventArgs
> (object,System.EventArgs)
> in <0x001c8> GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int) in
> <0x00094> (wrapper native-to-managed)
> GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int) in (unmanaged)
> (wrapper managed-to-native) Gtk.Application:gtk_main () in <0x00080>
> (wrapper managed-to-native) Gtk.Application:gtk_main () in <0x00014>
> Gtk.Application:Run () in <0x00044> 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
> http://lists.ximian.com/mailman/listinfo/mono-list

--=-1ZbDRCkDclFrKvr5Ixuz
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)

iD8DBQBBbocKi0GPOekbrtERAkzaAJ47WbvbgPDU00JoF1LtvSVEP1stYQCfebKQ
KEgnU/IIFLe2FxGurxujR0Y=
=0oEM
-----END PGP SIGNATURE-----

--=-1ZbDRCkDclFrKvr5Ixuz--