XmlResourceData.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Collections.Generic;
  2. using System.Xml;
  3. namespace OTSDataType
  4. {
  5. public struct StringRes
  6. {
  7. public int key;
  8. public string text;
  9. public string Description;
  10. }
  11. public class ResGroup
  12. {
  13. public int key;
  14. public string text = "";
  15. public string Description = "";
  16. public SortedDictionary<int, StringRes> mapRes = new SortedDictionary<int, StringRes>();
  17. }
  18. public class XmlResourceData
  19. {
  20. static XmlResourceData instance =new XmlResourceData();
  21. private SortedDictionary<int, ResGroup> resGroup = new SortedDictionary<int, ResGroup>();
  22. public XmlResourceData()
  23. {
  24. LoadStringFromXml();
  25. }
  26. public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des)
  27. {
  28. text = resGroup[grpKey].mapRes[itmKey].text;
  29. des = resGroup[grpKey].mapRes[itmKey].Description;
  30. }
  31. public string GetStringByKey(int grpKey, int itmKey)
  32. {
  33. return resGroup[grpKey].mapRes[itmKey].text;
  34. }
  35. public string GetStringByKey(int itmKey)
  36. {
  37. return resGroup[0].mapRes[itmKey].text;
  38. }
  39. public string GetGroupTextByKey(int grpKey)
  40. {
  41. return resGroup[grpKey].text;
  42. }
  43. public void GetGroupTextByKey(int grpKey, ref string text, ref string des)
  44. {
  45. text = resGroup[grpKey].text;
  46. des = resGroup[grpKey].Description;
  47. }
  48. public void SetStringByKey(int grpkey, int itmkey, string value, string des)
  49. {
  50. StringRes sr = new StringRes();
  51. sr.text = value;
  52. sr.Description = des;
  53. resGroup[grpkey].mapRes[itmkey] = sr;
  54. }
  55. public bool LoadStringFromXml()
  56. {
  57. XmlDocument xml = new XmlDocument();
  58. xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
  59. XmlNode root = xml.SelectSingleNode("Language");
  60. XmlNode root2 = root.SelectSingleNode("DefaultLanguage");
  61. string ss = root2.InnerText;
  62. XmlDocument doc1 = new XmlDocument();
  63. if (ss == "EN")
  64. {
  65. doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-EN.xml");//载入xml文件
  66. }
  67. else if (ss == "ZH")
  68. {
  69. doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-ZH.xml");//载入xml文件
  70. }
  71. root = doc1.SelectSingleNode("XMLData");
  72. root2 = root.SelectSingleNode("collection");
  73. //XmlDocument doc2 = new XmlDocument();
  74. //if (ss == "EN")
  75. //{
  76. // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-EN.xml");//载入xml文件
  77. //}
  78. //else if (ss == "ZH")
  79. //{
  80. // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-ZH.xml");//载入xml文件
  81. //}
  82. //XmlNode root1 = doc1.DocumentElement;
  83. ////合并两个Xml文档中的collection节点内容
  84. //root = doc2.SelectSingleNode("XMLData");
  85. //XmlNode xnl = root.SelectSingleNode("collection");
  86. //foreach (XmlNode xnItem in xnl)
  87. //{
  88. // XmlNode rootTemp = doc1.ImportNode(xnItem, true);
  89. // root2.AppendChild(rootTemp);
  90. //}
  91. XmlNodeList childlist = root2.ChildNodes;
  92. for (int i = 0; i < childlist.Count; i++)
  93. {
  94. int colkey = -1;
  95. if (childlist[i].Attributes["grpKey"] != null)
  96. {
  97. colkey = int.Parse(childlist[i].Attributes["grpKey"].Value);
  98. }
  99. string colText = "";
  100. if (childlist[i].Attributes["text"] != null)
  101. {
  102. colText = childlist[i].Attributes["text"].Value;
  103. }
  104. string colDes = "";
  105. if (childlist[i].Attributes["description"] != null)
  106. {
  107. colDes = childlist[i].Attributes["description"].Value;
  108. }
  109. ResGroup rg = new ResGroup();
  110. rg.key = colkey;
  111. rg.text = colText;
  112. rg.Description = colDes;
  113. XmlNodeList childlist2 = childlist[i].ChildNodes;
  114. for (int j = 0; j < childlist2.Count; j++)
  115. {
  116. StringRes sr = new StringRes();
  117. int key = int.Parse(childlist2[j].Attributes["itemKey"].Value);
  118. sr.key = key;
  119. sr.text = childlist2[j].Attributes["itemText"].Value;
  120. if (childlist2[j].Attributes["description"] != null)
  121. {
  122. sr.Description = childlist2[j].Attributes["description"].Value;
  123. }
  124. else
  125. {
  126. sr.Description = "空";
  127. }
  128. try
  129. {
  130. rg.mapRes[key] = sr;
  131. }
  132. catch (System.Exception ex)
  133. {
  134. // MessageBox.Show(ex.ToString());
  135. }
  136. }
  137. resGroup[colkey] = rg;
  138. }
  139. return true;
  140. }
  141. //public bool SaveStringToXml()
  142. //{
  143. // if (File.Exists(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml"))
  144. // {
  145. // XDocument xdoc = XDocument.Load(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  146. // IEnumerable<XElement> elements = from ele in xdoc.Descendants("collection") select ele;
  147. // foreach (var ele in elements)
  148. // {
  149. // if (ele != null)
  150. // {
  151. // ele.RemoveAll();
  152. // }
  153. // }
  154. // IEnumerable<XElement> cls = from ele in xdoc.Descendants("collection") select ele;
  155. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  156. // {
  157. // XElement EleName = new XElement("member");
  158. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  159. // EleName.SetAttributeValue("itemName", "");
  160. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  161. // cls.First().Add(EleName);
  162. // }
  163. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  164. // }
  165. // else
  166. // {
  167. // XDocument xdoc = new XDocument();
  168. // XElement root = new XElement("XMLData");
  169. // XElement cls = new XElement("collection");
  170. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  171. // {
  172. // XElement EleName = new XElement("member");
  173. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  174. // EleName.SetAttributeValue("itemName", "");
  175. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  176. // cls.Add(EleName);
  177. // }
  178. // root.Add(cls);
  179. // xdoc.Add(root);
  180. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  181. // }
  182. // return true;
  183. //}
  184. public static XmlResourceData GetInstance()
  185. {
  186. return instance;
  187. }
  188. }
  189. }