How to save a list of objects in isolated storage in wp7

社会主义新天地 提交于 2019-12-14 02:15:06

问题


Hi I have this class to save RSS feed items. I have a list of them and I want to store it in isolated storage in Windows phone 7. Can somebody help me for that. I know how to serialize the class and save it in the isolated storage as a single object for a single RSS item.

 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; }
}

Any help would be appreciated. Thanks.


回答1:


You can do it using xmlserializer.

code for saving your list is as follows:

 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);
         }

Code for retrieving is as follows:

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);
                }
            }
        }

You can also do it in Json format by using DataContractJsonSerializer class



来源:https://stackoverflow.com/questions/14016613/how-to-save-a-list-of-objects-in-isolated-storage-in-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!