前端之家收集整理的这篇文章主要介绍了
WinRT StorageFile 读写操作xml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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;
}
}
}