如何序列化SyndicationItem对象的集合?

时间:2011-11-01 15:27:11

标签: c# syndication-item

我有一个SyndicationItem对象的集合,我需要序列化,但我不知道我是怎么做的。建议?

IEnumberable<SyndicationItem> blogPosts = GetBlogPosts();

...

private IEnumerable<SyndicationItem> GetBlogPosts()
{
  var query = Enumerable.Empty<SyndicationItem>();
  foreach (string feed in Feeds)
  {
    try
    {
      using (XmlReader reader = XmlReader.Create(feed))
      {
        query = query.Union((from item in SyndicationFeed.Load(reader).Items
            orderby item.PublishDate descending
                select item).Take(NumberOfItemsToDisplay));
      }
    }
    catch (Exception ex)
    {
      Console.WriteLine("Invalid Feed: {0} ({1})", feed, ex.Message);
    }
  }
  query = query.OrderByDescending(i => i.PublishDate).Take(NumberOfItemsToDisplay);
  return query;
}

1 个答案:

答案 0 :(得分:0)

如果您想序列化SyndicationItem,可以使用 SaveAsRss20 SaveAsAtom10 的几种方法。要序列化集合,请逐个迭代并将其写入某个流

SyndicationItem item = new SyndicationItem("Item Title", "Item Content", new Uri("http://Item/Alternate/Link"), "itemID", DateTimeOffset.Now);
XmlWriter writer = XmlWriter.Create("outfile.xml");
item.SaveAsRss20(writer);
writer.Close();