[Mono-dev] serialization of structured data in ArrayList

Kornél Pál kornelpal at hotmail.com
Mon Dec 19 05:26:14 EST 2005


I never tried to serialize an ArrayList, but I think you had better to
serialize a strongly typed array of Car objects. An ArrayList is a too
complex structure. If you need an ArrayList on both sides of serialization,
you can use the constructor of ArrayList that takes an ICollection. Arrays
are ICollections so you can pass the received Car[] to the ArrayList
constructor.

Kornél

----- Original Message -----
From: "mirek" <Miroslav.Binas at cnl.tuke.sk>
To: "mirek" <Miroslav.Binas at cnl.tuke.sk>
Cc: <mono-devel-list at lists.ximian.com>
Sent: Monday, December 19, 2005 10:56 AM
Subject: Re: [Mono-dev] serialization of structured data in ArrayList


> when i compile the code below and i run it, i've got the following
> exception:
>
> Unhandled Exception: System.InvalidOperationException: The type Car was
> not expected. Use the XmlInclude or SoapInclude attribute to specify
> types that are not known statically.
>
> can you help me, how can i solve it? how to serialize the non primitive
> type (ArrayList with the instances of the Car class)?
>
> thanks
>
> mirek
> mirek wrote:
>
>> try to compile this code:
>>
>> using System;
>> using System.IO;
>> using System.Collections;
>> using System.Xml.Serialization;
>>
>> public class Car
>> {
>>  public string type;
>>  public int price;
>>
>>  public Car( string type, int price )
>>  {
>>     this.type = type;
>>     this.price = price;
>>  }
>> }
>>
>> public class Config
>> {
>>  static string filename = ".configrc";
>>
>>  public ArrayList list;
>>
>>  public Config()
>>  {
>>     list = new ArrayList();
>>  }
>>
>>  public void Add( string type, int price )
>>  {
>>     Car car = new Car( type, price );
>>     list.Add( car );
>>  }
>>
>>  public void Save( Config config )
>>  {
>>     using (FileStream fs = new FileStream(filename, FileMode.Create)) {
>>        XmlSerializer serializer = new XmlSerializer(typeof(Config));
>>        serializer.Serialize(fs, config);
>>     }
>>  }
>> }
>>
>>
>> class App
>> {
>>  public static void Main()
>>  {
>>     Config config = new Config();
>>     config.Add( "truck", 200 );
>>     config.Add( "pickup", 100 );
>>
>>     Console.WriteLine( "hello" );
>>
>>     config.Save( config );
>>
>>  }
>> }
>>
>>
>> mirek
>>
>> Robert Jordan wrote:
>>
>>> mirek wrote:
>>>
>>>> hello
>>>>
>>>> i try to use serialization for storing of configuration in my app.
>>>> the problem is, i've got some data stored in ArrayList, and they are
>>>> not primitive types - they are instances of classes or some structs.
>>>> so it looks like this:
>>>>
>>>>  [Serializable]
>>>>  public class Entry
>>>>  {
>>>>     public string type;
>>>>     public int price;
>>>>   }
>>>>
>>>>  [Serializable]
>>>>  public class CConfiguration
>>>>  {
>>>>     ...
>>>>     public ArrayList vehicle;
>>>>     ...
>>>>   }
>>>>
>>>>   // in my app...
>>>>   Entry e = new Entry();
>>>>   e.type = "truck";
>>>>   e.price = 200;
>>>>
>>>>   vehicle.Add( e );
>>>>
>>>> but it doesnt work. how can i do that?
>>>
>>>
>>>
>>> What doesn't work? Did your get an exception? If so, which one?
>>>
>>> Robert
>>>
>>> _______________________________________________
>>> Mono-devel-list mailing list
>>> Mono-devel-list at lists.ximian.com
>>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>
>>
>>
>>
>
>
> --
> e-mail: mirek at host.sk
> icq: 113397932
> jabber: mirek_b at jabber.sk
> homepage: http://www.intrak.sk/~binas
>
> english:
> Please avoid sending me Word or PowerPoint attachments.
> See http://www.fsf.org/philosophy/no-word-attachments.html
>
> slovak:
> prosim, neposielajte mi prilohy vo formatoch .doc a .ppt (power point)
> precitajte si http://www.fsf.org/philosophy/no-word-attachments.cs.html
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>




More information about the Mono-devel-list mailing list