[英]How to save a list of objects in isolated storage in wp7
Hi I have this class to save RSS feed items. 嗨,我有这个课程来保存RSS feed项目。 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.
我有一个列表,我想将其存储在Windows Phone 7的隔离存储中。有人可以帮我这个忙。 I know how to serialize the class and save it in the isolated storage as a single object for a single RSS item.
我知道如何序列化类并将其作为单个RSS项的单个对象保存在隔离存储中。
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.
谢谢。
You can do it using xmlserializer. 您可以使用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 您也可以通过使用DataContractJsonSerializer类以Json格式进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.