2012-12-24 165 views
2

嗨我有这个类来保存RSS提要项目。我有他们的列表,我想将它存储在Windows Phone 7的独立存储中。有人可以帮我解决这个问题。我知道如何序列化该类并将其作为单个RSS项目的单个对象保存在隔离存储中。如何保存wp7中隔离存储中的对象列表

public class RssItem 
{  
    public RssItem(string title, string summary, string publishedDate, string url ,string subtitle ,string duration, Enclosure enclosure) 
    { 
     Title = title; 
     Summary = summary; 
     PublishedDate = publishedDate; 
     Url = url; 
     Subtitle = subtitle; 
     Enclosure = enclosure; 
     Duration = duration; 
     PlainSummary = HttpUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", "")); 
    } 

    public string Title { get; set; } 
    public string Summary { get; set; } 
    public string PublishedDate { get; set; } 
    public string Url { get; set; } 
    public string PlainSummary { get; set; } 
    public Enclosure Enclosure { get; set; } 
    public string Description { get; set; } 
    public string Mp3Url { get; set; } 
    public string Subtitle { get; set; } 
    public string Duration { get; set; } 
} 

任何帮助,将不胜感激。谢谢。

+0

有无你认为一行一行还是以JSON保存? – onmyway133

+0

不,我还没有尝试过..但是有什么方法可以在隔离存储中直接保存列表吗? –

回答

4

您可以使用xmlserializer。为节省您的列表

代码如下:

var store = IsolatedStorageFile.GetUserStoreForApplication(); 
    if (store.FileExists(filePath)) 
      { 
       store.DeleteFile(filePath); 
      } 
     using (var stream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, store)) 
     { 
      var serializer = new XmlSerializer(typeof(List<RssItem>)); 
      serializer.Serialize(stream, RssItemsList); 
     } 

代码检索如下:

var store = IsolatedStorageFile.GetUserStoreForApplication(); 

     if (store.FileExists(filePath)) 
     { 
      using (var stream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read, store)) 
      { 
       var reader = new StreamReader(stream); 

       if (!reader.EndOfStream) 
       { 
        var serializer = new XmlSerializer(typeof(List<RssItem>)); 
         RssItemsList= (List<RssItem>)serializer.Deserialize(reader); 
       } 
      } 
     } 

你也可以做到这一点JSON格式使用DataContractJsonSerializer类

+0

你有没有试过这个代码..? – Swapnika

+0

嗨,我很抱歉迟到的回应,我会尝试这个代码,并希望这会帮助我。再次感谢。 –