2012-12-24 127 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

嗨,我很抱歉遲到的迴應,我會嘗試這個代碼,並希望這會幫助我。再次感謝。 –