tags:

views:

907

answers:

3

What's a simple way to combine feed and feed2? I want the items from feed2 to be added to feed. Also I want to avoid duplicates as feed might already have items when a question is tagged with both WPF and Silverlight.

Uri feedUri = new Uri("http://stackoverflow.com/feeds/tag/silverlight");
XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri);
SyndicationFeed feed = SyndicationFeed.Load(reader);    

Uri feed2Uri = new Uri("http://stackoverflow.com/feeds/tag/wpf");
XmlReader reader2 = XmlReader.Create(feed2Uri.AbsoluteUri);
SyndicationFeed feed2 = SyndicationFeed.Load(reader2);
A: 

If it's solely for stackoverflow, you can use this :
http://stackoverflow.com/feeds/tag/silverlight%20wpf
This will do an union of the two tags.

For a more general solution, I don't know. You'd probably have to manually iterate the elements of the two feeds and join them together. You can compare the <id> elements of <entry>s to see if they are duplicates.

David Thibault
I think that it will do an AND instead of an OR. I think I need to union the feeds.
Alan Le
I just checked and it's an OR
David Thibault
according to this post, it's doing an AND. I think I need to wait for the UNION functionality before I can use the RESTFUL tags uri. In the meantime I need iterate through it myself and do it manually. http://stackoverflow.com/questions/61590/stack-overflow-tag-filtering
Alan Le
+1  A: 

Well, one possibility is to create a new syndication feed that is a clone of the first feed, and then simply iterate through each post on the second one, check the first for its existence, and add it if it doesn't exist.

Something along the lines of:

SyndicationFeed newFeed = feed.clone;
foreach(SyndicationItem item in feed2.items)
{
  if (!newFeed.contains(item))
    newFeed.items.Add(item);
}

might be able to do it. It looks like 'items' is a simple enumberable list of syndication items, so theres not reason you can't simply add them.

Frater
I don't think a simple Contains() will do it. He'll probably have to compare entry IDs
David Thibault
RSS item elements are recommended to have a guid element, but it is not required. Will liklely need to determine inclusion when combining by first comparing guid. If guid not present, compare the link elements. May want to compare against title to be safe as well.
Oppositional
+6  A: 

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();
            }
        }
    }
}
Mike Brown
LINQ and UNION... that's genius. I was hoping that to not iterate through the feeds and this fits the bill.
Alan Le
Sweet! Thanks! +1
Charles
Genius solution. Thanks.
Roberto Bonini
Glad you're still finding a use for this :)
Mike Brown