public static MusicDataStore TestData() { MusicDataStore result = new MusicDataStore(); string[] artistNames = new string[] { "Rob Miles", "Fred Bloggs", "The Bloggs Singers", "Immy Brown" }; string[] titleNames = new string[] { "My Way", "Your Way", "His Way", "Her Way", "Milky Way" }; Random rand = new Random(1); foreach (string artistName in artistNames) { Artist newArtist = new Artist { Name = artistName }; result.Artists.Add(newArtist); foreach (string titleName in titleNames) { MusicTrack newTrack = new MusicTrack { Artist = newArtist, Title = titleName, Length = rand.Next(20, 600) }; result.MusicTracks.Add(newTrack); } } return(result); }
static void Main(string[] args) { MusicDataStore musicData = MusicDataStore.TestData(); BinaryFormatter formatter = new BinaryFormatter(); using (FileStream outputStream = new FileStream("MusicTracks.bin", FileMode.OpenOrCreate, FileAccess.Write)) { formatter.Serialize(outputStream, musicData); } MusicDataStore inputData; using (FileStream inputStream = new FileStream("MusicTracks.bin", FileMode.Open, FileAccess.Read)) { inputData = (MusicDataStore)formatter.Deserialize(inputStream); } Console.WriteLine(inputData); Console.ReadKey(); }