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