Exemplo n.º 1
0
 /// <summary>
 /// Sort the items by date and truncate the number of feed items if it exceeds the 
 /// NumberOfItems property.
 /// </summary>
 public virtual void SortAndFilter()
 {
     FeedItem[] feedItemArray = new FeedItem[this._feedItems.Count];
     this._feedItems.CopyTo(feedItemArray, 0);
     Array.Sort(feedItemArray);
     // Little dirty: clear FeedItems and add the first NumberOfItems from the sorted array.
     this._feedItems.Clear();
     int i = 0;
     while (i < feedItemArray.Length && i < this._numberOfItems)
     {
         this._feedItems.Add(feedItemArray[i]);
         i++;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Refresh the items of a single feed. It doesn't save the new items!
        /// </summary>
        /// <param name="feed"></param>
        public void RefreshFeedContents(Feed feed)
        {
            feed.FeedItems.Clear();
            try
            {
                XmlDocument doc = GetFeedXml(feed.Url);
                // Create an XmlNamespaceManager for resolving namespaces.
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

                XmlNodeList xmlItems = doc.SelectNodes("//channel/item");
                foreach (XmlNode xmlItem in xmlItems)
                {
                    FeedItem feedItem = new FeedItem();
                    // Truncate title to 100 characters
                    string title = xmlItem.SelectSingleNode("title").InnerText;
                    if (title.Length > 100)
                    {
                        title = title.Substring(0, 100);
                    }
                    feedItem.Title = title;
                    feedItem.Url = xmlItem.SelectSingleNode("link").InnerText;

                    if (xmlItem.SelectSingleNode("pubDate") != null)
                    {
                        feedItem.PubDate = RFC2822Date.Parse(xmlItem.SelectSingleNode("pubDate").InnerText);
                    }
                    else
                    {
                        feedItem.PubDate = DateTime.Now;
                    }
                    if (this._showContents && xmlItem.SelectSingleNode("description") != null)
                    {
                        feedItem.Content = xmlItem.SelectSingleNode("description").InnerText;
                    }
                    if (xmlItem.SelectSingleNode("author", nsmgr) != null)
                    {
                        feedItem.Author = xmlItem.SelectSingleNode("author", nsmgr).InnerText;
                    }
                    else if (xmlItem.SelectSingleNode("dc:creator", nsmgr) != null)
                    {
                        feedItem.Author = xmlItem.SelectSingleNode("dc:creator", nsmgr).InnerText;
                    }
                    feedItem.Feed = feed;
                    feed.FeedItems.Add(feedItem);
                }
                feed.SortAndFilter();
            }
            catch (Exception ex)
            {
                log.Error("Error refreshing feed contents for: " + feed.Title, ex);
                throw;
            }
        }