2011-11-01 39 views
1

我有我需要序列化的SyndicationItem對象的集合,但我不知道我如何去做這件事。建議?如何序列化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; 
} 

回答

0

如果你想序列化SyndicationItem,它有幾個方法,你可以使用SaveAsRss20SaveAsAtom10。要序列化收集,逐個迭代並將其寫入一些流

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(); 
+0

感謝您的建議。雖然這確實有效,但我認爲這種方法對於我的需求是過分的。最後,我決定讓我的Linq查詢返回一組簡單的POCO對象,因爲我並不需要SyndicationItem提供的所有數據。 – eponymous23

+0

如果我們需要SyndicationItem提供的所有數據,該怎麼辦? – Wingjam