Thank you very much for the example. Now, I solve de problem but I have had to solve a bug of mcs too.
http://bugzilla.ximian.com/show_bug.cgi?id=61358 Regards, Sergio El vie, 15-10-2004 a las 11:10 +0200, RoBiK escribi�: > Hi, > > I see another problem here. It is not an good idea to derive from ArrayList, > because the XmlSerializer has special handling for ArrayList and also > classes that derive from it. Instead you should Package the ArrayList indo > 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 any > instances of custom objets, that you are inserting into the ArrayList. This > is done by the XmlInclude attribute. > Try this example: > > 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 = new Subject(); > subject.name = "Subject Name"; > subject.comment = "Subject Comment"; > SubjectList subjectList = new SubjectList(); > subjectList.subject = subject; > subjectList.list.Add("Test"); > subjectList.list.Add(123); > CustomObject co = new CustomObject(); > co.customString = "Custom String"; > subjectList.list.Add(co); > XmlSerializer serializer = new > XmlSerializer(typeof(SubjectList)); > StringBuilder sb = new StringBuilder(); > StringWriter sw = 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 = new ArrayList(); > } > } > public class Subject > { > public string name; > public string comment; > } > public class CustomObject > { > public string customString; > } > } > > Robert > > -----Original Message----- > From: Sergio Paracuellos [mailto:[EMAIL PROTECTED] > Sent: Donnerstag, 14. Oktober 2004 16:03 > To: RoBiK > Cc: [EMAIL PROTECTED] > Subject: RE: [Mono-list] Problem with xml.Serialization > > El jue, 14-10-2004 a las 15:42 +0200, RoBiK escribi�: > > 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 > > 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? > > Thanks! > > > > > > > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Sergio > > Paracuellos > > Sent: Donnerstag, 14. Oktober 2004 15:27 > > To: [EMAIL PROTECTED] > > 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,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 > > (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> > > 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 > > > > _______________________________________________ > > Mono-list maillist - [EMAIL PROTECTED] > > http://lists.ximian.com/mailman/listinfo/mono-list > > _______________________________________________ > Mono-list maillist - [EMAIL PROTECTED] > http://lists.ximian.com/mailman/listinfo/mono-list
signature.asc
Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada digitalmente
