Example #1
0
        private async Task ReadSongsAsync(XmlReader reader)
        {
            await reader.ReadAsync();

            while (reader.LocalName.Equals(Constants.Db.Song))
            {
                Logger.Debug("Reading a song");

                UInt32 id;
                if (!UInt32.TryParse(reader.GetAttribute(Constants.Db.Id), out id))
                {
                    Logger.Debug("Could not read song id");
                    await reader.ReadAsync();

                    continue;
                }

                if (reader.IsEmptyElement)
                {
                    Logger.Debug("Song element is empty");
                    await reader.ReadAsync();

                    continue;
                }

                await reader.ReadAsync();

                if (reader.IsEndElement(Constants.Db.Song))
                {
                    Logger.Debug("Song element is followed by end element");
                    reader.ReadEndElement();
                    continue;
                }

                reader.ConfirmElement(Constants.Db.SongTitle);
                var title = await reader.TryGetContentAsync();

                reader.ConfirmElement(Constants.Db.Location);
                var location = await reader.TryGetContentAsync();

                reader.ConfirmElement(Constants.Db.Milliseconds);
                int milliseconds;
                if (!int.TryParse(await reader.TryGetContentAsync(), out milliseconds))
                {
                    Logger.Error("Song {0} has a missing or unreadable millisecond value", title);
                }

                reader.ConfirmElement(Constants.Db.PlayCount);
                uint playCount;
                if (!uint.TryParse(await reader.TryGetContentAsync(), out playCount))
                {
                    Logger.Error("Song {0} has a missing or unreadable playcount", title);
                }

                reader.ConfirmElement(Constants.Db.Bpm);
                var guess = Convert.ToBoolean(reader.GetAttribute(Constants.Db.Guess));
                int bpmValue;
                if (!int.TryParse(await reader.TryGetContentAsync(), out bpmValue))
                {
                    Logger.Error("Song {0} has a missing or unreadable BPM value", title);
                }

                var song = new Song(title, location, milliseconds, new Bpm(bpmValue, guess),
                                    playCount, id);
                SongDb.AddSong(song);

                reader.ConfirmElement(Constants.Db.Tags);
                if (reader.IsEmptyElement)
                {
                    await reader.ReadAsync();
                }
                else
                {
                    await ReadSongTagsAsync(reader, song);

                    reader.ReadEndElement();
                }

                reader.ReadEndElement();
            }
        }