Exemple #1
0
        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);
            int    IDcount = 0;

            foreach (string artistName in artistNames)
            {
                Artist newArtist = new Artist {
                    ID = IDcount++, Name = artistName
                };
                result.Artists.Add(newArtist);
                foreach (string titleName in titleNames)
                {
                    MusicTrack newTrack = new MusicTrack
                    {
                        ID       = IDcount++,
                        ArtistID = newArtist.ID,
                        Title    = titleName,
                        Length   = rand.Next(20, 600)
                    };
                    result.MusicTracks.Add(newTrack);
                }
            }
            return(result);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            MusicDataStore musicData = MusicDataStore.TestData();

            XmlSerializer formatter = new XmlSerializer(typeof(MusicDataStore));

            using (FileStream outputStream =
                       new FileStream("MusicTracks.xml", FileMode.OpenOrCreate, FileAccess.Write))
            {
                formatter.Serialize(outputStream, musicData);
            }

            MusicDataStore inputData;

            using (FileStream inputStream =
                       new FileStream("MusicTracks.xml", FileMode.Open, FileAccess.Read))
            {
                inputData = (MusicDataStore)formatter.Deserialize(inputStream);
            }

            Console.WriteLine(inputData);

            Console.ReadKey();
        }