将 Feed 和 feed2 结合起来的简单方法是什么?我希望将 feed2 中的项目添加到 Feed 。另外,我想避免重复,因为当使用WPF和Silverlight标记问题时, feed 可能已经有项目。
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);
答案 0 :(得分:11)
您可以使用LINQ来简化代码以加入两个列表(不要忘记将System.Linq放入您的使用中,如果需要,请参考项目中的System.Core)这是一个Main,它将联合并打印到控制台(正确清理读卡器)。
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();
}
}
}
}
答案 1 :(得分:1)
嗯,有一种可能性是创建一个新的联合供稿,它是第一个供稿的克隆,然后简单地遍历第二个上的每个帖子,检查第一个是否存在,如果不存在则添加它存在。
有些事情:
SyndicationFeed newFeed = feed.clone;
foreach(SyndicationItem item in feed2.items)
{
if (!newFeed.contains(item))
newFeed.items.Add(item);
}
也许能够做到。看起来“项目”是一个简单易懂的联合项目列表,因此不能仅仅添加它们。
答案 2 :(得分:0)
如果它仅用于stackoverflow,您可以使用:
https://stackoverflow.com/feeds/tag/silverlight%20wpf
将执行两个标记的并集。
对于更一般的解决方案,我不知道。您可能必须手动迭代两个Feed的元素并将它们连接在一起。您可以比较< id> < entry>的元素,以查看它们是否重复。
答案 3 :(得分:0)
我已将今天接受的答案转变为单元测试,只是为了稍微探讨一下:
[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<bool>(
() =>
{
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.");
}
答案 4 :(得分:0)
//Executed and Tested :)
using (XmlReader reader = XmlReader.Create(strFeed))
{
rssData = SyndicationFeed.Load(reader);
model.BlogFeed = rssData; ;
}
using (XmlReader reader = XmlReader.Create(strFeed1))
{
rssData1 = SyndicationFeed.Load(reader);
model.BlogFeed = rssData1;
}
SyndicationFeed feed3 = new SyndicationFeed(rssData.Items.Union(rssData1.Items));
model.BlogFeed = feed3;
return View(model);
答案 5 :(得分:0)
这对我来说很好:
// create temporary List of SyndicationItem's
List<SyndicationItem> tempItems = new List<SyndicationItem>();
// add all feed items to the list
tempItems.AddRange(feed.Items);
tempItems.AddRange(feed2.Items);
// remove duplicates with Linq 'Distinct()'-method depending on yourattributes
// add list without duplicates to 'feed2'
feed2.Items = tempItems