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
原创粉丝点击