XmlResourceData.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Xml;
  5. namespace OTSDataType
  6. {
  7. public struct StringRes
  8. {
  9. public int key;
  10. public string text;
  11. public string Description;
  12. }
  13. public class ResGroup
  14. {
  15. public int key;
  16. public string text = "";
  17. public string Description = "";
  18. public SortedDictionary<int, StringRes> mapRes = new SortedDictionary<int, StringRes>();
  19. }
  20. public class XmlResourceData
  21. {
  22. static XmlResourceData instance =new XmlResourceData();
  23. private SortedDictionary<int, ResGroup> resGroup = new SortedDictionary<int, ResGroup>();
  24. public XmlResourceData()
  25. {
  26. LoadStringFromXml();
  27. }
  28. public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des)
  29. {
  30. if (!resGroup.ContainsKey(grpKey))
  31. {
  32. return;
  33. }
  34. if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
  35. {
  36. return;
  37. }
  38. text = resGroup[grpKey].mapRes[itmKey].text;
  39. des = resGroup[grpKey].mapRes[itmKey].Description;
  40. }
  41. public string GetStringByKey(int grpKey, int itmKey)
  42. {
  43. if (!resGroup.ContainsKey(grpKey))
  44. {
  45. return "";
  46. }
  47. if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
  48. {
  49. return "";
  50. }
  51. return resGroup[grpKey].mapRes[itmKey].text;
  52. }
  53. public string GetGroupTextByKey(int grpKey)
  54. {
  55. if (!resGroup.ContainsKey(grpKey))
  56. {
  57. return "";
  58. }
  59. return resGroup[grpKey].text;
  60. }
  61. public void GetGroupTextByKey(int grpKey, ref string text, ref string des)
  62. {
  63. if (!resGroup.ContainsKey(grpKey))
  64. {
  65. return ;
  66. }
  67. text = resGroup[grpKey].text;
  68. des = resGroup[grpKey].Description;
  69. }
  70. public void SetStringByKey(int grpKey, int itmKey, string value, string des)
  71. {
  72. if (!resGroup.ContainsKey(grpKey))
  73. {
  74. return;
  75. }
  76. if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
  77. {
  78. return;
  79. }
  80. StringRes sr = new StringRes();
  81. sr.text = value;
  82. sr.Description = des;
  83. resGroup[grpKey].mapRes[itmKey] = sr;
  84. }
  85. public bool LoadStringFromXml()
  86. {
  87. XmlDocument xml = new XmlDocument();
  88. string path= Application.StartupPath + @"\Resources\XMLData\LanguageDefine.xml";
  89. if (!File.Exists(path))
  90. {
  91. NLog.LogManager.GetCurrentClassLogger().Error("There's no \\Resources\\XMLData\\LanguageDefine.xml");
  92. return false;
  93. }
  94. xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
  95. XmlNode root = xml.SelectSingleNode("Language");
  96. XmlNode root2 = root.SelectSingleNode("DefaultLanguage");
  97. string ss = root2.InnerText;
  98. XmlDocument doc1 = new XmlDocument();
  99. if (ss == "EN")
  100. {
  101. doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-EN.xml");//载入xml文件
  102. }
  103. else if (ss == "ZH")
  104. {
  105. doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-ZH.xml");//载入xml文件
  106. }
  107. root = doc1.SelectSingleNode("XMLData");
  108. root2 = root.SelectSingleNode("collection");
  109. XmlNodeList childlist = root2.ChildNodes;
  110. for (int i = 0; i < childlist.Count; i++)
  111. {
  112. int colkey = -1;
  113. if (childlist[i].Attributes["grpKey"] != null)
  114. {
  115. colkey = int.Parse(childlist[i].Attributes["grpKey"].Value);
  116. }
  117. string colText = "";
  118. if (childlist[i].Attributes["text"] != null)
  119. {
  120. colText = childlist[i].Attributes["text"].Value;
  121. }
  122. string colDes = "";
  123. if (childlist[i].Attributes["description"] != null)
  124. {
  125. colDes = childlist[i].Attributes["description"].Value;
  126. }
  127. ResGroup rg = new ResGroup();
  128. rg.key = colkey;
  129. rg.text = colText;
  130. rg.Description = colDes;
  131. XmlNodeList childlist2 = childlist[i].ChildNodes;
  132. for (int j = 0; j < childlist2.Count; j++)
  133. {
  134. StringRes sr = new StringRes();
  135. int key = int.Parse(childlist2[j].Attributes["itemKey"].Value);
  136. sr.key = key;
  137. sr.text = childlist2[j].Attributes["itemText"].Value;
  138. if (childlist2[j].Attributes["description"] != null)
  139. {
  140. sr.Description = childlist2[j].Attributes["description"].Value;
  141. }
  142. else
  143. {
  144. sr.Description = "空";
  145. }
  146. try
  147. {
  148. rg.mapRes[key] = sr;
  149. }
  150. catch (System.Exception ex)
  151. {
  152. NLog.LogManager.GetCurrentClassLogger().Warn(ex.Message);
  153. }
  154. }
  155. resGroup[colkey] = rg;
  156. }
  157. return true;
  158. }
  159. public static XmlResourceData GetInstance()
  160. {
  161. return instance;
  162. }
  163. }
  164. }