XmlResourceData.cs 6.8 KB

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