public override void FromXml(XElement albumXml) { // ReSharper disable PossibleNullReferenceException Name = albumXml.Element("name").Value; if (albumXml.Element("artist") != null) { ArtistName = albumXml.Element("artist").HasElements ? albumXml.Element("artist").Element("name").Value : albumXml.Element("artist").Value; } MusicBrainzId = albumXml.Element("mbid").Value; Url = new Uri(albumXml.Element("url").Value, UriKind.RelativeOrAbsolute); if (albumXml.Element("releasedate") != null && !string.IsNullOrEmpty(albumXml.Element("releasedate").Value)) ReleaseDate = Convert.ToDateTime(albumXml.Element("releasedate").Value); string uriString = albumXml.Descendants("image") .First(descendant => descendant.Attribute("size").Value == "medium") .Value; if (!string.IsNullOrEmpty(uriString)) Picture = new BitmapImage(new Uri(uriString)); uriString = albumXml.Descendants("image") .First(descendant => descendant.Attribute("size").Value == "small") .Value; if (!string.IsNullOrEmpty(uriString)) PictureSmall = new BitmapImage(new Uri(uriString)); uriString = albumXml.Descendants("image") .LastOrDefault() .Value; if (!string.IsNullOrEmpty(uriString)) PictureLarge = new BitmapImage(new Uri(uriString)); Listeners = albumXml.Element("listeners") == null ? 0 : Convert.ToInt32(albumXml.Element("listeners").Value); PlayCount = albumXml.Element("playcount") == null ? 0 : Convert.ToInt32(albumXml.Element("playcount").Value); Wiki = new Biography(albumXml.Descendants("wiki").FirstOrDefault()); var tags = albumXml.Descendants("tag"); Tags = new List<Tag>(tags.Count()); foreach (var tag in tags) Tags.Add(new Tag(tag)); var tracks = albumXml.Descendants("track"); Tracks = new List<Track>(tracks.Count()); foreach (var track in tracks) Tracks.Add(new Track(track)); // ReSharper restore PossibleNullReferenceException }
public override void FromXml(XElement trackXml) { // ReSharper disable PossibleNullReferenceException if (trackXml.Attribute("rank") != null && !string.IsNullOrEmpty(trackXml.Attribute("rank").Value)) { Rank = Convert.ToInt32(trackXml.Attribute("rank").Value); } Name = trackXml.Element("name").Value; if (string.IsNullOrEmpty(trackXml.Element("mbid").Value)) MusicBrainzId = trackXml.Element("mbid").Value; if (trackXml.Element("duration") != null && !string.IsNullOrEmpty(trackXml.Element("duration").Value)) { Duration = TimeSpan.FromMilliseconds(Convert.ToDouble(trackXml.Element("duration").Value)); } Url = new Uri(trackXml.Element("url").Value); if (trackXml.Element("streamable") != null) { Streamable = trackXml.Element("streamable").Value != "0"; } if (trackXml.Descendants("artist").FirstOrDefault() != null) { if (trackXml.Descendants("artist").FirstOrDefault().HasElements) { ArtistName = trackXml.Descendants("artist").FirstOrDefault().Element("name").Value; if (trackXml.Descendants("artist").FirstOrDefault().Element("mbid") != null) ArtistMusicBrainzId = trackXml.Descendants("artist").FirstOrDefault().Element("mbid").Value; } else { ArtistName = trackXml.Descendants("artist").FirstOrDefault().Value; } } if (trackXml.Descendants("album").FirstOrDefault() != null && trackXml.Descendants("album").FirstOrDefault().HasElements) { Position = Convert.ToInt32(trackXml.Descendants("album").FirstOrDefault().Attribute("position").Value); AlbumName = trackXml.Descendants("album").FirstOrDefault().Element("title").Value; if (trackXml.Descendants("album").FirstOrDefault().Element("mbid") != null) AlbumMusicBrainzId = trackXml.Descendants("album").FirstOrDefault().Element("mbid").Value; } if (trackXml.Element("image") == null) { if (trackXml.Descendants("album").FirstOrDefault() != null && trackXml.Descendants("album").FirstOrDefault().HasElements) { var images = trackXml.Descendants("album").FirstOrDefault().Descendants("image"); foreach (var element in images) { switch (element.Attribute("size").Value) { case "medium": if (!string.IsNullOrEmpty(element.Value)) { PictureSmall = new BitmapImage(new Uri(element.Value)); PictureLarge = Picture; } break; case "large": if (!string.IsNullOrEmpty(element.Value)) { Picture = new BitmapImage(new Uri(element.Value)); PictureLarge = Picture; } break; case "extralarge": if (!string.IsNullOrEmpty(element.Value)) PictureLarge = new BitmapImage(new Uri(element.Value)); break; } } } } else { if (trackXml.Descendants("image").FirstOrDefault() != null && trackXml.Descendants("image").FirstOrDefault(descendant => descendant.Attribute("size").Value == "large") != null) { string uriString = trackXml.Descendants("image") .FirstOrDefault(descendant => descendant.Attribute("size").Value == "large") .Value; if (!string.IsNullOrEmpty(uriString)) Picture = new BitmapImage(new Uri(uriString)); } if (trackXml.Descendants("image").FirstOrDefault() != null && trackXml.Descendants("image").FirstOrDefault(descendant => descendant.Attribute("size").Value == "medium") != null) { string uriString = trackXml.Descendants("image") .FirstOrDefault(descendant => descendant.Attribute("size").Value == "medium") .Value; if (!string.IsNullOrEmpty(uriString)) PictureSmall = new BitmapImage(new Uri(uriString)); } if (trackXml.Descendants("image").FirstOrDefault() != null) { string uriString = trackXml.Descendants("image") .LastOrDefault() .Value; if (!string.IsNullOrEmpty(uriString)) PictureLarge = new BitmapImage(new Uri(uriString)); } } Listeners = trackXml.Element("listeners") == null ? 0 : Convert.ToInt32(trackXml.Element("listeners").Value); PlayCount = trackXml.Element("playcount") == null ? 0 : Convert.ToInt32(trackXml.Element("playcount").Value); Tags = new List<Tag>(); foreach (var tagXml in trackXml.Descendants("tag")) Tags.Add(new Tag(tagXml)); Wiki = new Biography(trackXml.Descendants("wiki").FirstOrDefault()); if (trackXml.Element("match") == null || string.IsNullOrEmpty(trackXml.Element("match").Value)) { SimilarMatch = 0; } else { SimilarMatch = (int)Math.Round(Convert.ToDouble(trackXml.Element("match").Value), 0); } // ReSharper restore PossibleNullReferenceException }