internal static int itExist(string str, Library lib)
 {
     for (int i = 0; i < lib.songs.Count(); i++)
     {
         if (str == lib.songs[i].title)
             return i;
     }
     return -1;
 }
        private void update(Library library)
        {
            // formating songs with N/A when the field is empty
            for (int i = 0; i < this.library.songs.Count(); i++)
                this.library.songs[i] = this.library.songs[i].formatSong();
            // adding songs to albums
            for (int i = 0; i < this.library.songs.Count(); i++)
            {
                int index;
                if ((index = Search.itExist(this.library.songs[i].album, this.library.albums)) != -1)
                    this.library.albums[index].addSong(this.library.songs[i]);
                else
                    this.library.albums.Add(new Album(this.library.songs[i].album, this.library.songs[i].artist, new List<Song>()));
            }

            // adding artists to albums
            for (int i = 0; i < this.library.albums.Count(); i++)
            {
                for (int j = 0; j < this.library.albums[i].songs.Count(); j++)
                {
                    for (int k = 0; k < this.library.albums[i].songs[j].artist.Count(); k++)
                    {
                        int index;
                        if ((index = Search.itExist(this.library.albums[i].songs[j].artist[k], this.library.albums[i].artists)) == -1)
                            this.library.albums[i].artists.Add(this.library.albums[i].songs[j].artist[k]);
                    }
                }
            }

            //adding albums to artists
            for (int i = 0; i < this.library.albums.Count(); i++)
            {
                for (int j = 0; j < this.library.albums[i].artists.Count(); j++)
                {
                    int index;
                    if ((index = Search.itExist(this.library.albums[i].artists[j], this.library.artists)) == -1)
                    {
                        this.library.artists.Add(new Artist(this.library.albums[i].artists[j], new List<Album>()));
                        this.library.artists.Last().albums.Add(this.library.albums[i]);
                    }
                    else if ((Search.itExist(this.library.albums[i].name, this.library.artists[index].albums)) == -1)
                        this.library.artists[index].albums.Add(this.library.albums[i]);
                }
            }
        }
 public void saveLib(Library lib)
 {
     XmlSerializer xs = new XmlSerializer(typeof(Library));
     using (TextWriter writer = new StreamWriter(@"\temp\library.xml"))
     {
         xs.Serialize(writer, lib);
     }
 }
 public ServiceLib(Library lib)
 {
     this.library = lib;
     if (File.Exists(@"\Temp\library.xml"))
         this.loadLib(@"\Temp\library.xml");
 }
 public void loadLib(string filePath)
 {
     XmlSerializer xs = new XmlSerializer(typeof(Library));
     using (var sr = new StreamReader(filePath))
     {
         Library lib = (Library)xs.Deserialize(sr);
         this.library = lib;
     }
 }