[Mono-list] Problem with xml.Serialization

RoBiK robik@mailbox.sk
Thu, 14 Oct 2004 15:42:51 +0200


As the error message says: The type gorganizer.Subject was not expected. Use
the XmlInclude or SoapInclude attribute to specify types that are not known
statically.
You are assinging an instance of type gorganizer.Subject to a "obj" variable
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 not
expected. So either use the XmlInlude attribude to tell the serializer what
to expect, or rework your code to use another aproach.

Robert



-----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

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,bool
)
in <0x00ca0>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberElemen
t (System.Xml.Serialization.XmlTypeMapElementInfo,object)
in <0x004f8>
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteListContent
(System.Xml.Serialization.TypeData,System.Xml.Serialization.ListMap,object,S
ystem.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,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.XmlSerializerNamespace
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 ()

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 = new FileStream("data.xml", FileMode.Create)) {
     XmlSerializer serializer = 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);
            }
        }
 	
	public int Length () {
            return this.Count;
        }

        /*
         * Add an object to list if doesn't exists
         */
        public bool AddToList(object obj) {
            if (obj != 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 != 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 = null;
            Subject s = null;
            IEnumerator i = this.GetEnumerator();
            while (i.MoveNext()) {
                if (i.Current is Subject) {
                    aux = (Subject) i.Current;
                    if (aux.Name.Equals(name)) {
                        s = aux;
                    }
                }
            }
            return s;
        }

        private void PrintValues (ArrayList myList) {
            IEnumerator myEnumerator = 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 = value;
            }
        }

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

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

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

Any Idea of what it's happening?

Thanks to all,

Regards,

	Sergio Paracuellos