XmlResourceData.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //XmlDocument doc2 = new XmlDocument();
  110. //if (ss == "EN")
  111. //{
  112. // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-EN.xml");//载入xml文件
  113. //}
  114. //else if (ss == "ZH")
  115. //{
  116. // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-ZH.xml");//载入xml文件
  117. //}
  118. //XmlNode root1 = doc1.DocumentElement;
  119. ////合并两个Xml文档中的collection节点内容
  120. //root = doc2.SelectSingleNode("XMLData");
  121. //XmlNode xnl = root.SelectSingleNode("collection");
  122. //foreach (XmlNode xnItem in xnl)
  123. //{
  124. // XmlNode rootTemp = doc1.ImportNode(xnItem, true);
  125. // root2.AppendChild(rootTemp);
  126. //}
  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. ResGroup rg = new ResGroup();
  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. StringRes sr = new StringRes();
  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. else
  161. {
  162. sr.Description = "空";
  163. }
  164. try
  165. {
  166. rg.mapRes[key] = sr;
  167. }
  168. catch (System.Exception ex)
  169. {
  170. // MessageBox.Show(ex.ToString());
  171. }
  172. }
  173. resGroup[colkey] = rg;
  174. }
  175. return true;
  176. }
  177. //public bool SaveStringToXml()
  178. //{
  179. // if (File.Exists(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml"))
  180. // {
  181. // XDocument xdoc = XDocument.Load(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  182. // IEnumerable<XElement> elements = from ele in xdoc.Descendants("collection") select ele;
  183. // foreach (var ele in elements)
  184. // {
  185. // if (ele != null)
  186. // {
  187. // ele.RemoveAll();
  188. // }
  189. // }
  190. // IEnumerable<XElement> cls = from ele in xdoc.Descendants("collection") select ele;
  191. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  192. // {
  193. // XElement EleName = new XElement("member");
  194. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  195. // EleName.SetAttributeValue("itemName", "");
  196. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  197. // cls.First().Add(EleName);
  198. // }
  199. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  200. // }
  201. // else
  202. // {
  203. // XDocument xdoc = new XDocument();
  204. // XElement root = new XElement("XMLData");
  205. // XElement cls = new XElement("collection");
  206. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  207. // {
  208. // XElement EleName = new XElement("member");
  209. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  210. // EleName.SetAttributeValue("itemName", "");
  211. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  212. // cls.Add(EleName);
  213. // }
  214. // root.Add(cls);
  215. // xdoc.Add(root);
  216. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  217. // }
  218. // return true;
  219. //}
  220. public static XmlResourceData GetInstance()
  221. {
  222. return instance;
  223. }
  224. }
  225. }