[Mono-list] Problem with xml.Serialization

Sergio Paracuellos par@dragon-lance.net
Thu, 14 Oct 2004 15:27:13 +0200


--=-W5lQrm4sYMiqJ9zZu24m
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Hi!

I'm doing an application and I need to serialize in xml a list of
objects. I get the following error:

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,boo=
l)
in <0x00ca0>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberEleme=
nt (System.Xml.Serialization.XmlTypeMapElementInfo,object)
in <0x004f8>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListContent=
 (System.Xml.Serialization.TypeData,System.Xml.Serialization.ListMap,object=
,System.Text.StringBuilder)
in <0x0024c>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListElement=
 (System.Xml.Serialization.XmlTypeMapping,object,string,string)
in <0x0050c>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
(System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,boo=
l)
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.XmlSerializerNamespac=
es)
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 ()

A resume of the code:

When I push save button:

 private void OnSaveClicked (object o, EventArgs args) {
     this.SaveDataToXml();
 }


private void SaveDataToXml () {
  using (FileStream fs =3D new FileStream("data.xml", FileMode.Create)) {
     XmlSerializer serializer =3D new XmlSerializer(typeof(SubjectList));
     serializer.Serialize(fs, mySubjectList);
  }
}

* SubjectList.cs:

using System;
using System.Collections;
using System.Xml.Serialization;

namespace gorganizer {

    public class SubjectList: ArrayList, IMyList {
        private object obj;
        private int length;

        //For Serialization
        public SubjectList (){}

        public Subject Obj {
            get {
                return ((Subject)obj);
            }
        }
 =09
	public int Length () {
            return this.Count;
        }

        /*
         * 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;
        }

        /*
         * 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;
        }

 public void ListElements (ArrayList myList) {
            this.PrintValues(myList);
        }

        /* 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;
        }

        private void PrintValues (ArrayList myList) {
            IEnumerator myEnumerator =3D myList.GetEnumerator();
            while (myEnumerator.MoveNext())
                Console.WriteLine(myEnumerator.Current.ToString());
        }
    }
}

Subject.cs:

using System;
using System.Xml.Serialization;

namespace gorganizer {

    public class Subject {

        //Constructor
        public Subject () {
        }

        private string name;
        public string Name {
            get {
                return name;
            }
            set {
                name =3D value;
            }
        }

        private TimeTable theory;
        public TimeTable Theory {
            get {
                return theory;
            }
            set {
                theory =3D value;
            }
        }

        private string comment;
        public string Comment {
            get {
                return comment;
            }
            set {
                comment =3D value;
            }
        }

        public void DeleteFields () {
            name =3D null;
            theory =3D null;
            comment =3D null;
        }
    }
}

Any Idea of what it's happening?

Thanks to all,

Regards,

	Sergio Paracuellos

--=-W5lQrm4sYMiqJ9zZu24m
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)

iD8DBQBBbn6wi0GPOekbrtERAqJLAKCmfUWJZkP4zTTjG/mwb2qtvkLLtACffNLT
GkVwj2myRb188CsCoRmOdSQ=
=U+h1
-----END PGP SIGNATURE-----

--=-W5lQrm4sYMiqJ9zZu24m--