I've successfully used "union" as described here to join two RSS feeds in a c# project, but we have a scenario where we could have up to a hundred RSS feeds to join together. What would be the best way with that number of feeds?
A:
- Create an XML Document with a single root node
- Read the Children of the root node of all secondary RSS feeds
- Append those children to the New XML Documents root node
To get around the "node already belongs to another document" issue, simply take the Inner XML from the root node, and append it to the Inner XML of the aggregated document.
Tom Anderson
2009-09-17 19:33:59
+1
A:
I would use SelectMany()
(shown here via LINQ) to flatten the feeds into a single sequence and then use Distinct()
to filter out duplicates you've already seen:
var feeds = new[] {
"http://stackoverflow.com/feeds/tag/silverlight",
"http://stackoverflow.com/feeds/tag/wpf"
};
var items = from url in feeds
from xr in XmlReader.Create(url).Use()
let feed = SyndicationFeed.Load(xr)
from i in feed.Items
select i;
var newFeed = new SyndicationFeed(items.Distinct());
Use()
is an extension method described here to clean up the reader after it's used. You also might need to define your own IEqualityComparer<SyndicationItem>
to use with Distinct()
.
dahlbyk
2009-09-17 20:28:28