WinRT StorageFile 读写操作xml

前端之家收集整理的这篇文章主要介绍了WinRT StorageFile 读写操作xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Windows.Data.Xml.Dom;
  6. using Windows.Storage;
  7.  
  8. namespace Microsoft.Assistant.MobileAssistantRT.Utilities
  9. {
  10. public class LocalFolderHelper
  11. {
  12. private const string STORE_CONFIG_NAME = "Config.xml";
  13. private const string STORE_CONFIG_FOLDERNAME = "Data";
  14. private static StorageFolder localFolder = ApplicationData.Current.LocalFolder;
  15.  
  16. /// <summary>
  17. /// Create the storeConfig.xml if it need.
  18. /// </summary>
  19. /// <typeparam name="T">the Generics type</typeparam>
  20. /// <param name="t">the type to save</param>
  21. public async static Task CreateStoreConifgXML<T>(T t)
  22. {
  23. StorageFolder storageFolder = await localFolder.CreateFolderAsync(STORE_CONFIG_FOLDERNAME,CreationCollisionOption.OpenIfExists);
  24. StorageFile storageFile = await storageFolder.CreateFileAsync(STORE_CONFIG_NAME,CreationCollisionOption.ReplaceExisting);
  25. var properties = t.GetType().GetTypeInfo().DeclaredProperties;
  26. XmlDocument dom = new XmlDocument();
  27. XmlElement xmlRootNode;
  28.  
  29. xmlRootNode = dom.CreateElement("Configuration");
  30. dom.AppendChild(xmlRootNode);
  31.  
  32. XmlElement x1 = dom.CreateElement(t.GetType().Name);
  33.  
  34. foreach (var property in properties)
  35. {
  36. XmlElement xmlElement = dom.CreateElement(property.Name);
  37.  
  38. if (property.GetValue(t) != null)
  39. {
  40. xmlElement.InnerText = property.GetValue(t).ToString();
  41. }
  42. x1.AppendChild(xmlElement);
  43. }
  44. xmlRootNode.AppendChild(x1);
  45.  
  46. await dom.SaveToFileAsync(storageFile);
  47. }
  48.  
  49. /// <summary>
  50. /// Check whether the storeConfig.xml exists.
  51. /// path: C:\Users\UserName\AppData\Local\Packages\packageName\LocalState\Data\Config.xml
  52. /// </summary>
  53. /// <returns>return null if not exists.</returns>
  54. public async static Task<StorageFile> CheckStoreConfigExist()
  55. {
  56. try
  57. {
  58. StorageFolder folderFound = await localFolder.GetFolderAsync(STORE_CONFIG_FOLDERNAME);
  59. StorageFile fileFound = await folderFound.GetFileAsync(STORE_CONFIG_NAME);
  60.  
  61. return fileFound;
  62. }
  63. catch (Exception e)
  64. {
  65. return null;
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// Read config from xml
  71. /// </summary>
  72. /// <typeparam name="T">The type to return</typeparam>
  73. /// <param name="storageFile">the data source</param>
  74. /// <param name="t">the object</param>
  75. /// <returns></returns>
  76. public async static Task<T> GetDataFromConifgXML<T>(StorageFile storageFile,T t)
  77. {
  78. if (storageFile == null)
  79. {
  80. return default(T);
  81. }
  82.  
  83. var stream = await storageFile.OpenAsync(FileAccessMode.Read);
  84. XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(storageFile);
  85. XmlNodeList nodes = xmlDoc.SelectNodes(@"/Configuration/" + t.GetType().Name);
  86. IXmlNode node = nodes.FirstOrDefault();
  87. if (node == null)
  88. {
  89. return default(T);
  90. }
  91. var properties = t.GetType().GetTypeInfo().DeclaredProperties;
  92.  
  93. if (node != null && node.HasChildNodes())
  94. {
  95. var list = node.ChildNodes;
  96. foreach (var item in list)
  97. {
  98. PropertyInfo prop = properties.Where((i) => i.Name.Equals(item.LocalName)).FirstOrDefault();
  99. if (prop != null)
  100. {
  101. int newInt;
  102. Guid newGuid = Guid.Empty;
  103. if (prop.PropertyType.Name.Equals("Int32") && Int32.TryParse(item.InnerText,out newInt))
  104. {
  105. prop.SetValue(t,newInt);
  106. }
  107. // some property maybe nullable.
  108. else if (prop.PropertyType.FullName.Contains("Guid") && Guid.TryParse(item.InnerText,out newGuid))
  109. {
  110. prop.SetValue(t,newGuid);
  111. }
  112. else
  113. {
  114. prop.SetValue(t,item.InnerText);
  115. }
  116. }
  117. }
  118.  
  119. }
  120.  
  121. return (T)t;
  122. }
  123. }
  124. }

猜你在找的XML相关文章