开发者

Writing entries from online XML file to isolated storage

I have a simple Silverlight WP7 app that reads the contents of an online XML file and displays them. However, there are maybe 150 items total and scrolling through them all can be quite inconvenient. So, I want make a sort of "Favorites" p开发者_StackOverflow中文版age. When you click on any item on the list, it writes it to a separate XML file within the app package. After it writes to that XML, I need to make sure it still updates the list each time the app is loaded, instead of strictly saving the contents of that entry at the time it was written. What method would be the best way to go about this?


First you get hold of the relevant IsolatedStorageFile using IsolateStorageFile.GetUserStoreForApplication() and then create a file using IsolatedStorageFile.CreateFile. That returns a Stream which you can write to in the normal way:

using (var storage = IsolateStorageFile.GetUserStoreForApplication())
using (var stream = storage.CreateFile("test.xml"))
{
    doc.Save(stream); // Where doc is an XDocument
}

It's as simple as that.

One key point is to avoid thinking of IsolatedStorageFile as a single file - think of it as being a whole drive for your application. It can contain files, directories etc. It may end up being a single file in the native file system, but your application doesn't need to know or care about that.


You could store the items to an XML file in the IsolatedStorage, just like you would do for any other file.

When the application is loaded, you need to set the actions in the Application_Launching event handler (in App.xaml.cs) - for example, you could create a List that will be bound to a ListView element that will get contain elements from the loaded XML file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜