WinRT StorageFile 读写操作xml
来源:互联网 发布:pdf 编辑软件 编辑:程序博客网 时间:2024/06/06 02:56
using System;using System.Linq;using System.Reflection;using System.Threading.Tasks;using Windows.Data.Xml.Dom;using Windows.Storage;namespace Microsoft.Assistant.MobileAssistantRT.Utilities{ public class LocalFolderHelper { private const string STORE_CONFIG_NAME = "Config.xml"; private const string STORE_CONFIG_FOLDERNAME = "Data"; private static StorageFolder localFolder = ApplicationData.Current.LocalFolder; /// <summary> /// Create the storeConfig.xml if it need. /// </summary> /// <typeparam name="T">the Generics type</typeparam> /// <param name="t">the type to save</param> public async static Task CreateStoreConifgXML<T>(T t) { StorageFolder storageFolder = await localFolder.CreateFolderAsync(STORE_CONFIG_FOLDERNAME, CreationCollisionOption.OpenIfExists); StorageFile storageFile = await storageFolder.CreateFileAsync(STORE_CONFIG_NAME, CreationCollisionOption.ReplaceExisting); var properties = t.GetType().GetTypeInfo().DeclaredProperties; XmlDocument dom = new XmlDocument(); XmlElement xmlRootNode; xmlRootNode = dom.CreateElement("Configuration"); dom.AppendChild(xmlRootNode); XmlElement x1 = dom.CreateElement(t.GetType().Name); foreach (var property in properties) { XmlElement xmlElement = dom.CreateElement(property.Name); if (property.GetValue(t) != null) { xmlElement.InnerText = property.GetValue(t).ToString(); } x1.AppendChild(xmlElement); } xmlRootNode.AppendChild(x1); await dom.SaveToFileAsync(storageFile); } /// <summary> /// Check whether the storeConfig.xml exists. /// path: C:\Users\UserName\AppData\Local\Packages\packageName\LocalState\Data\Config.xml /// </summary> /// <returns>return null if not exists.</returns> public async static Task<StorageFile> CheckStoreConfigExist() { try { StorageFolder folderFound = await localFolder.GetFolderAsync(STORE_CONFIG_FOLDERNAME); StorageFile fileFound = await folderFound.GetFileAsync(STORE_CONFIG_NAME); return fileFound; } catch (Exception e) { return null; } } /// <summary> /// Read config from xml /// </summary> /// <typeparam name="T">The type to return</typeparam> /// <param name="storageFile">the data source</param> /// <param name="t">the object</param> /// <returns></returns> public async static Task<T> GetDataFromConifgXML<T>(StorageFile storageFile, T t) { if (storageFile == null) { return default(T); } var stream = await storageFile.OpenAsync(FileAccessMode.Read); XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(storageFile); XmlNodeList nodes = xmlDoc.SelectNodes(@"/Configuration/" + t.GetType().Name); IXmlNode node = nodes.FirstOrDefault(); if (node == null) { return default(T); } var properties = t.GetType().GetTypeInfo().DeclaredProperties; if (node != null && node.HasChildNodes()) { var list = node.ChildNodes; foreach (var item in list) { PropertyInfo prop = properties.Where((i) => i.Name.Equals(item.LocalName)).FirstOrDefault(); if (prop != null) { int newInt; Guid newGuid = Guid.Empty; if (prop.PropertyType.Name.Equals("Int32") && Int32.TryParse(item.InnerText, out newInt)) { prop.SetValue(t, newInt); } // some property maybe nullable. else if (prop.PropertyType.FullName.Contains("Guid") && Guid.TryParse(item.InnerText, out newGuid)) { prop.SetValue(t, newGuid); } else { prop.SetValue(t, item.InnerText); } } } } return (T)t; } }}
0 0
- WinRT StorageFile 读写操作xml
- xml的读写操作
- xml文件操作( 读写)
- DOM读写XML操作
- XOM读写XML操作
- XML文件读写操作
- XML文件读写操作
- xml 读写操作
- xml读写操作
- xml linq 读写操作
- XML读写操作
- 简单XML读写操作
- 读写文件操作xml
- C# xml 读写操作
- XML读写操作
- JDom操作XML文件读写
- java xml的读写操作!
- opencv 操作XML,读写数组
- Android 性能优化之数据库优化
- 利用xslt对xml进行缩进格式化处理
- oracle 12
- 开放封闭原则
- Jmail的属性和方法
- WinRT StorageFile 读写操作xml
- 给你一个MD5算法
- java中引用传递和值传送的区别
- 如何显示数据库中以 OLE 对象方式存储的 JPEG 图片
- Makefile中使用shell注意点
- C++入门之谈前置++与后置++的区别
- HTML 学习笔记(二)
- greenplum数据库引擎探究
- 查看JDK和Eclipse位数