Combining two SyndicationFeeds

后端 未结 6 1564
無奈伤痛
無奈伤痛 2021-02-08 13:05

What\'s a simple way to combine feed and feed2? I want the items from feed2 to be added to feed. Also I want

6条回答
  •  伪装坚强ぢ
    2021-02-08 13:29

    I've turned today's accepted answer into a unit test just to explore this slightly:

        [TestMethod]
        public void ShouldCombineRssFeeds()
        {
            //reference: http://stackoverflow.com/questions/79197/combining-two-syndicationfeeds
    
            SyndicationFeed feed;
            SyndicationFeed feed2;
    
            var feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight");
            using(var reader = XmlReader.Create(feedUri.AbsoluteUri))
            {
                feed = SyndicationFeed.Load(reader);
            }
    
            Assert.IsTrue(feed.Items.Count() > 0, "The expected feed items are not here.");
    
            var feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf");
            using(var reader2 = XmlReader.Create(feed2Uri.AbsoluteUri))
            {
                feed2 = SyndicationFeed.Load(reader2);
            }
    
            Assert.IsTrue(feed2.Items.Count() > 0, "The expected feed items are not here.");
    
            var feedsCombined = new SyndicationFeed(feed.Items.Union(feed2.Items));
    
            Assert.IsTrue(
                feedsCombined.Items.Count() == feed.Items.Count() + feed2.Items.Count(),
                "The expected number of combined feed items are not here.");
    
            var builder = new StringBuilder();
            using(var writer = XmlWriter.Create(builder))
            {
                feedsCombined.SaveAsRss20(writer);
                writer.Flush();
                writer.Close();
            }
    
            var xmlString = builder.ToString();
    
            Assert.IsTrue(new Func(
                () =>
                {
                    var test = false;
    
                    var xDoc = XDocument.Parse(xmlString);
                    var count = xDoc.Root.Element("channel").Elements("item").Count();
                    test = (count == feedsCombined.Items.Count());
    
                    return test;
                }
            ).Invoke(), "The expected number of RSS items are not here.");
        }
    

提交回复
热议问题