Exemplo n.º 1
0
        /// <summary>
        /// Creates the feed.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        private void CreateFeed(VacancySearchDetailForRssDTO parameters)
        {
            var vacancyController = new VacancyLogicService(new Data.VacancyDataService());
            var results           = vacancyController.GetVacanciesForRss(parameters);

            SyndicationFeed feed  = null;
            var             items = new List <SyndicationItem>();

            if (results != null)
            {
                feed = new SyndicationFeed(results.FeedTitle, results.FeedDescription, new Uri(results.AlternateLink));

                if (!string.IsNullOrWhiteSpace(results.FeedImageUrl))
                {
                    feed.ImageUrl = new Uri(results.FeedImageUrl);
                }

                if (!string.IsNullOrWhiteSpace(results.FeedCopyrightInformation))
                {
                    feed.Copyright = new TextSyndicationContent(results.FeedCopyrightInformation);
                }

                feed.Links.Add(new SyndicationLink(new Uri(Request.Url.ToString()), "self", null, "application/rss+xml", 1000));

                //foreach and LINQ statements are slower than for loop
                //even if they are more readable, and as performance is critical, I have stuck with for.
                for (var i = 0; i < results.Vacancies.Count; i++)
                {
                    var vacancyTitle = results.Vacancies[i].VacancyTitle;
                    var vacancyDesc  = FormatFeed(results.Vacancies[i]);

                    string url = QueryStringHelper.CreateVacancyUrlForRssFeed(results.Vacancies[i]);

                    //Now create the RSS item
                    var item = new SyndicationItem(vacancyTitle, vacancyDesc, new Uri(url));
                    item.Categories.Add(new SyndicationCategory(results.Vacancies[i].ApprenticeshipFramework.Description));
                    item.Id = url;
                    //As it is using the vacancy DTO to deliver the results, possible start date is actually date the vacancy went live.
                    item.PublishDate = results.Vacancies[i].PossibleStartDate;


                    items.Add(item);
                }
            }

            if (feed == null)
            {
                return;
            }

            feed.Items = items;

            SyndicationFeedFormatter formatter;
            string format = HttpContext.Current.Request.QueryString["format"] ?? "";

            if (format.Trim().ToLower() == "atom")
            {
                Response.ContentType = "application/atom+xml";
                formatter            = new Atom10FeedFormatter(feed);
            }
            else
            {
                Response.ContentType = "application/rss+xml";
                formatter            = new Rss20FeedFormatter(feed);
            }
            var settings = new XmlWriterSettings {
                NewLineHandling    = NewLineHandling.None,
                Indent             = true,
                Encoding           = Encoding.UTF32,
                ConformanceLevel   = ConformanceLevel.Document,
                OmitXmlDeclaration = true
            };
            var buffer       = new StringBuilder();
            var cachedOutput = new StringWriter(buffer);

            using (XmlWriter writer = XmlWriter.Create(cachedOutput, settings))
            {
                if (writer != null)
                {
                    formatter.WriteTo(writer);
                    writer.Close();
                }
            }

            buffer.Replace(" xmlns:a10=\"http://www.w3.org/2005/Atom\"", " xmlns:atom=\"http://www.w3.org/2005/Atom\"");
            buffer.Replace("a10:", "atom:");

            Response.Output.Write(buffer.ToString());
        }