您好我有这个课程来保存RSS提要项目。我有一个列表,我想将它存储在Windows Phone 7的独立存储中。有人可以帮助我。我知道如何序列化类并将其作为单个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; }
}
任何帮助将不胜感激。感谢。
答案 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);
}
}
}
您也可以使用DataContractJsonSerializer类
以Json格式执行此操作