Windows phone IsolatedStorageFile 读写XML
2014-04-25 15:51
218 查看
using System; using System.IO; using System.IO.IsolatedStorage; using System.Xml; using System.Xml.Serialization; namespace CommonUI.CommonHelper { public static class IsolatedStorageFileHelper { private const string CONFIG_NAME = "Config.xml"; private static bool CheckStoreConfigExist() { using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { return isolatedStorage.FileExists(CONFIG_NAME); } } /// <summary> /// Save data to XML file /// </summary> /// <typeparam name="T">the type to save</typeparam> /// <param name="t">the data the save</param> public static void CreateStoreConifgXML<T>(T t) { using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { // Write to the Isolated Storage XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; if (CheckStoreConfigExist()) { isolatedStorage.DeleteFile(CONFIG_NAME); } using (IsolatedStorageFileStream isoStream = isolatedStorage.CreateFile(CONFIG_NAME)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, settings)) { serializer.Serialize(xmlWriter, t); } } } } /// <summary> /// Read data from config /// </summary> /// <typeparam name="T">the type to get</typeparam> /// <returns>the data get</returns> public static T GetDataFromConifgXML<T>(T t) { T result = default(T); try { using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (!CheckStoreConfigExist()) { return result; } using (IsolatedStorageFileStream isoStream = isolatedStorage.OpenFile(CONFIG_NAME, FileMode.Open)) { using (StreamReader reader = new StreamReader(isoStream)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); result = (T)serializer.Deserialize(reader); } } } } catch (Exception isoException) { //Nothing to do, only to prevent app crash here. } return result; } } }
相关文章推荐
- [手机资讯]Windows Phone 7首款机型8月25日开卖
- Windows Phone 7开发一月谈(8)
- 黑马程序员---面向对象
- 黑马程序员---异常处理异常
- 黑马程序员---委托和事件
- 黑马程序员---文件的读写
- 黑马程序员---集合
- 微软首次展示Windows 8:开始界面类似WP7(图)
- 8个Windows Phone开发工具
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- 使用VS2010玩跨界,android,ios,WP.
- Windows Phone 7 Series Developer General FAQ
- 使用Marketplace Deeplinking来推广你的Windows Phone应用
- 微软放出针对Windows Phone 7 Series的App Bar的图标库
- Windows Phone 7 开发机解锁过程
- WP7 Dev 101 【0】 开发环境的准备
- Windows Phone 7 Emulator常用快捷键
- WP7 Dev 101 【2】 你所要了解的一些数字
- WP7 Dev 101 【3】 向Windows Phone 7手机部署应用程序
- 是时候更换你的桌面音乐播放软件了