public XDocument EncodeSmartPlaylist(string name, bool matchAnyRule, SmartPlaylistLimit limit, IList <SmartPlaylistRule> rules) { XDocument doc = new XDocument( new XElement(XmlElementSmartPlaylist, new XElement(XmlElementName) { Value = name }, new XElement(XmlElementMatch) { Value = matchAnyRule ? MatchAny : MatchAll }, new XElement(XmlElementLimit, new XAttribute(XmlAttributeType, SmartPlaylistLimit.TypeToString(limit.Type))) { Value = limit.Value.ToString() }, rules.Select(x => new XElement(XmlElementRule, new XAttribute(XmlAttributeField, x.Field), new XAttribute(XmlAttributeOperator, x.Operator)) { Value = x.Value }) ) ); return(doc); }
public DecodeSmartPlaylistResult DecodePlaylist(string fileName) { if (!System.IO.Path.GetExtension(fileName.ToLower()).Equals(FileFormats.DSPL)) { return(new DecodeSmartPlaylistResult { DecodeResult = new OperationResult { Result = false } }); } OperationResult decodeResult = new OperationResult { Result = true }; string playlistName = string.Empty; string match = string.Empty; // string order = string.Empty; // TODO: order by SmartPlaylistLimit limit = new SmartPlaylistLimit(SmartPlaylistLimitType.Songs, 0); IList <SmartPlaylistRule> rules = new List <SmartPlaylistRule>(); try { XDocument xdoc = XDocument.Load(fileName); // Name XElement nameElement = (from t in xdoc.Element(XmlElementSmartPlaylist).Elements(XmlElementName) select t).FirstOrDefault(); playlistName = nameElement != null ? nameElement.Value : string.Empty; // Match XElement matchElement = (from t in xdoc.Element(XmlElementSmartPlaylist).Elements(XmlElementMatch) select t).FirstOrDefault(); match = matchElement != null ? matchElement.Value : string.Empty; // Order //XElement orderElement = (from t in xdoc.Element(XmlElementSmartPlaylist).Elements(XmlElementOrder) // select t).FirstOrDefault(); // order = orderElement != null ? orderElement.Value : string.Empty; // Limit XElement limitElement = (from t in xdoc.Element(XmlElementSmartPlaylist).Elements(XmlElementLimit) select t).FirstOrDefault(); if (limitElement != null && !string.IsNullOrEmpty(limitElement.Attribute(XmlAttributeType).Value)) { int limitValue = 0; if (limitElement.Attribute(XmlAttributeType) != null && int.TryParse(limitElement.Value, out limitValue)) { limit = new SmartPlaylistLimit(SmartPlaylistLimit.StringToType(limitElement.Attribute(XmlAttributeType).Value), limitValue); } } // Rules IList <XElement> ruleElements = (from t in xdoc.Element(XmlElementSmartPlaylist).Elements(XmlElementRule) select t).ToList(); if (ruleElements == null || ruleElements.Count == 0) { throw new Exception($"{nameof(ruleElements)} is null or contains no elements"); } foreach (XElement ruleElement in ruleElements) { rules.Add(new SmartPlaylistRule(ruleElement.Attribute(XmlAttributeField).Value, ruleElement.Attribute(XmlAttributeOperator).Value, ruleElement.Value)); } } catch (Exception ex) { LogClient.Error($"Could not decode smart playlist '{fileName}'. Exception: {ex.Message}"); decodeResult.Result = false; } return(new DecodeSmartPlaylistResult { DecodeResult = decodeResult, PlaylistName = playlistName, Match = match, Limit = limit, Rules = rules }); }