Combining two SyndicationFeeds

后端 未结 6 1668
無奈伤痛
無奈伤痛 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:47

    You can use LINQ to simplify the code to join two lists (don't forget to put System.Linq in your usings and if necessary reference System.Core in your project) Here's a Main that does the union and prints them to console (with proper cleanup of the Reader).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.ServiceModel.Syndication;
    
    namespace FeedUnion
    {
        class Program
        {
            static void Main(string[] args)
            {
                Uri feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight"); 
                SyndicationFeed feed;
                SyndicationFeed feed2;
                using(XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
                {
                    feed= SyndicationFeed.Load(reader); 
                }
                Uri feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf"); 
                using (XmlReader reader2 = XmlReader.Create(feed2Uri.AbsoluteUri))
                {
                feed2 = SyndicationFeed.Load(reader2);
                }
                SyndicationFeed feed3 = new SyndicationFeed(feed.Items.Union(feed2.Items));
                StringBuilder builder = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(builder))
                {
                    feed3.SaveAsRss20(writer);
                    System.Console.Write(builder.ToString());
                    System.Console.Read();
                }
            }
        }
    }
    

提交回复
热议问题