[Mono-dev] serialization of structured data in ArrayList

mirek Miroslav.Binas at cnl.tuke.sk
Sun Dec 18 13:35:45 EST 2005


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




More information about the Mono-devel-list mailing list