Resources.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //------------------------------------------------------------
  2. // All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd.
  3. //------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. namespace Ant.Service.Utilities
  9. {
  10. /// <summary>
  11. /// BUResourceManager
  12. /// 资源管理器
  13. ///
  14. /// 修改纪录
  15. /// 2007.05.16 版本:1.0 JiRiGaLa 重新调整代码的规范化。
  16. ///
  17. /// 版本:1.0
  18. ///
  19. /// <author>
  20. /// <name>JiRiGaLa</name>
  21. /// <date>2007.05.16</date>
  22. /// </author>
  23. /// </summary>
  24. [XmlRoot("resources")]
  25. public class Resources
  26. {
  27. private SortedList<String, String> indexs = new SortedList<String, String>();
  28. [XmlElement("language")]
  29. public string language = string.Empty;
  30. [XmlElement("displayName")]
  31. public string displayName = string.Empty;
  32. [XmlElement("version")]
  33. public string version = string.Empty;
  34. [XmlElement("author")]
  35. public string author = string.Empty;
  36. [XmlElement("description")]
  37. public string description = string.Empty;
  38. [XmlElement("items", typeof(Items))]
  39. public Items items;
  40. public void createIndex()
  41. {
  42. indexs.Clear();
  43. if (items == null)
  44. {
  45. return;
  46. }
  47. indexs = new SortedList<String, String>(items.items.Length);
  48. for (int i = 0; i < items.items.Length; i++)
  49. {
  50. #if DEBUG
  51. try
  52. {
  53. indexs.Add(items.items[i].key, items.items[i].value);
  54. }
  55. catch
  56. {
  57. throw (new Exception(items.items[i].key + items.items[i].value));
  58. }
  59. #else
  60. indexs.Add(items.items[i].key, items.items[i].value);
  61. #endif
  62. }
  63. }
  64. public string Get(string key)
  65. {
  66. if (!indexs.ContainsKey(key))
  67. {
  68. return string.Empty;
  69. }
  70. return indexs[key];
  71. }
  72. /// <summary>
  73. /// JiRiGaLa 2007.05.02
  74. /// </summary>
  75. /// <param name="key"></param>
  76. /// <param name="value"></param>
  77. /// <returns></returns>
  78. public bool Set(string key, string value)
  79. {
  80. if (!indexs.ContainsKey(key))
  81. {
  82. return false;
  83. }
  84. indexs[key] = value;
  85. for (int i = 0; i < items.items.Length; i++)
  86. {
  87. if (items.items[i].key == key)
  88. {
  89. items.items[i].value = value;
  90. break;
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. public class Items
  97. {
  98. [XmlElement("item", typeof(Item))]
  99. public Item[] items;
  100. }
  101. public class Item
  102. {
  103. [XmlAttribute("key")]
  104. public string key = string.Empty;
  105. [XmlText]
  106. public string value = string.Empty;
  107. }
  108. internal class ResourcesSerializer
  109. {
  110. public static Resources DeSerialize(string filePath)
  111. {
  112. System.Xml.Serialization.XmlSerializer XmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Resources));
  113. System.IO.FileStream FileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
  114. Resources Resources = XmlSerializer.Deserialize(FileStream) as Resources;
  115. FileStream.Close();
  116. return Resources;
  117. }
  118. public static void Serialize(string filePath, Resources Resources)
  119. {
  120. System.Xml.Serialization.XmlSerializer XmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Resources));
  121. System.IO.FileStream FileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
  122. XmlSerializer.Serialize(FileStream, Resources);
  123. FileStream.Close();
  124. }
  125. }
  126. }