如何保存在WP7隔离存储对象的列表 [英] How to save a list of objects in isolated storage in wp7
问题描述
您好我有这个类来保存RSS源项目。我对他们有一个名单,我想将其存储在独立存储在Windows Phone 7的人能帮我这一点。我知道如何序列化类,并在独立存储保存它作为一个单一的RSS项单个对象。
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; }
}
任何帮助将是AP preciated。谢谢你。
Any help would be appreciated. Thanks.
推荐答案
您可以使用XmlSerializer的做到这一点。
You can do it using xmlserializer.
code为节省您的名单如下:
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检索如下:
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);
}
}
}
您也可以做到这一点JSON格式使用DataContractJsonSerializer类
You can also do it in Json format by using DataContractJsonSerializer class
这篇关于如何保存在WP7隔离存储对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!