Serialize Collection of Object in C#

// August 14th, 2009 // Programming

Today i’ve create a simple project… finally I find way to serialize collection of object in C#.

As luck would have it, most of the types found within the System.Collections and System.Collections.Generic namespaces have already been marked as [Serializable]. Therefore, if you wish to persist a set of objects, simply add the set to the container (such as an ArrayList or a List) and serialize the object to your stream of choice. Other choice is make a class that inherits from collection…

first we can create a simple SoundData class like this :

[Serializable]
public class SoundData
{
   private int soundId;
   private string soundName;

   public int SoundId
   {
        get { return soundId; }
        set { soundId = value; }
    }

   public string SoundName
   {
        get { return soundName; }
        set { soundName = value; }
   }

   //You will get error if you miss this constructor ^_^
   public SoundData()
   { }

   public SoundData(int soundId, string soundName)
   {
       this.soundId = soundId;
       this.soundName = soundName;
   }
}


Then we make new collection class that use SoundData class as item :

[Serializable]
public class ListSoundData : Collection
{
    private SoundData sound;

    public SoundData Sound
    {
        get { return sound; }
        set { sound = value; }
    }

    public ListSoundData()
    { }

    public ListSoundData(SoundData sound)
    {
        this.sound = sound;
    }
}

Now you can add this code to Serialize and Deserialize code :

[Serializable,
XmlRoot(Namespace = "http://wcode.net")]
public class SoundMainData
{
    private int lastId;
    private ListSoundData listOfSound = new ListSoundData();

    public int LastId
    {
        get { return lastId; }
        set { lastId = value; }
    }

    public ListSoundData ListOfSound
    {
        get { return listOfSound; }
        set { listOfSound = value;}
    }

    public int Add(string soundName)
    {
        lastId++;
        SoundData sData = new SoundData(lastId, soundName);
        this.ListOfSound.Add(sData);
        return lastId;
    }

    public void Save()
    {
        Stream stream = File.Create("result.xml");
        XmlSerializer serializer = new XmlSerializer(typeof(SoundMainData),
        new Type[] { typeof(ListSoundData), typeof(SoundData) });
        serializer.Serialize(stream, this);
        stream.Close();
    }

    public void Load()
    {
        this.ListOfSound.Clear();
        Stream stream = new FileStream("result.xml",
        FileMode.Open, FileAccess.Read, FileShare.Read);
        XmlSerializer serializer = new XmlSerializer(typeof(SoundMainData));

        SoundMainData data = (SoundMainData)serializer.Deserialize(stream);

        foreach (SoundData sound in data.ListOfSound)
        {
               this.ListOfSound.Add(sound);
         }
         this.LastId = data.LastId;
     }
}

If you add SoundData in ListSoundData than Serialize it with this code:

SoundMainData data = new SoundMainData();
data.Add("willy");
data.Add("achmat");
data.Save();

Than you will have an result.xml file that contains this code :



  2
  
    
      1
      willy
    
    
      2
      achmat
    
  

by : Willy Achmat Fauzi

2 Responses to “Serialize Collection of Object in C#”

  1. NOTaMango says:

    Very shorts, simple and easy to understand, bet some more comments from your side would be great

  2. Ina Hoang says:

    Wow am I literally the only reply to your awesome writing?!?

Leave a Reply

PHVsPjxsaT48c3Ryb25nPndvb19hYm91dDwvc3Ryb25nPiAtIDwvbGk+PGxpPjxzdHJvbmc+d29vX2Fib3V0bGluazwvc3Ryb25nPiAtICM8L2xpPjxsaT48c3Ryb25nPndvb19hZHNfcm90YXRlPC9zdHJvbmc+IC0gZmFsc2U8L2xpPjxsaT48c3Ryb25nPndvb19hZF9pbWFnZV8xPC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tL2Fkcy93b290aGVtZXMtMTI1eDEyNS0xLmdpZjwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX2ltYWdlXzI8L3N0cm9uZz4gLSBodHRwOi8vd3d3Lndvb3RoZW1lcy5jb20vYWRzL3dvb3RoZW1lcy0xMjV4MTI1LTIuZ2lmPC9saT48bGk+PHN0cm9uZz53b29fYWRfaW1hZ2VfMzwvc3Ryb25nPiAtIGh0dHA6Ly93d3cud29vdGhlbWVzLmNvbS9hZHMvd29vdGhlbWVzLTEyNXgxMjUtMy5naWY8L2xpPjxsaT48c3Ryb25nPndvb19hZF9pbWFnZV80PC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tL2Fkcy93b290aGVtZXMtMTI1eDEyNS00LmdpZjwvbGk+PGxpPjxzdHJvbmc+d29vX2FkX3VybF8xPC9zdHJvbmc+IC0gaHR0cDovL3d3dy53b290aGVtZXMuY29tPC9saT48bGk+PHN0cm9uZz53b29fYWR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