using System; using System.Linq; using System.Xml.Linq; using System.Collections.Generic; using Aggregator.Classes; namespace Aggregator.Parser { /// /// Class to provide Parsing services /// public static class Parser { /// /// Parse a single xml code, given a config, to Articles /// /// XML code from an RSS feed /// Config that tells parser where to get relevant information /// Parsed articles from XML code public static IEnumerable
Parse(string xml, Config config) { return XDocument.Parse(xml).Descendants("item") //make an XDocument out of the xml .Select(articles => articles.Descendants().ToArray()) //turn each 'item' into it's respective descendants .Select(article => new Article() //make a new article from each 'item' article, using config for pointers { Source = config.Source, Title = article[config.Title].Value, Description = article[config.Description].Value, ArticleLink = article[config.ArticleLink].Value, ImageLink = article[config.ImageLink].Attribute("url").Value, PublishDate = DateTime.Parse(article[config.PublishDate].Value) }).Distinct(); //remove any duplicates } public static IEnumerable
ParseMultiple(string[] xmls, List configs) { if (configs.Count == 0 || xmls.Length == 0 || (xmls.Length != configs.Count)) { return null; //for bad cases } else { return xmls.Select((xml, index) => Parse(xml, configs[index])) //Parse each item .SelectMany(i => i); //remove distinctions between each set of articles } } } }