XmlResourceData.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. public string DownList;
  11. }
  12. public class ResGroup
  13. {
  14. public int key;
  15. public string text = "";
  16. public string Description = "";
  17. public string DownList = "";
  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,ref string downlist)
  29. {
  30. text = resGroup[grpKey].mapRes[itmKey].text;
  31. des = resGroup[grpKey].mapRes[itmKey].Description;
  32. downlist = resGroup[grpKey].mapRes[itmKey].DownList;
  33. }
  34. public string GetStringByKey(int grpKey, int itmKey)
  35. {
  36. return resGroup[grpKey].mapRes[itmKey].text;
  37. }
  38. public string GetStringByKey(int itmKey)
  39. {
  40. return resGroup[0].mapRes[itmKey].text;
  41. }
  42. public string GetGroupTextByKey(int grpKey)
  43. {
  44. return resGroup[grpKey].text;
  45. }
  46. public void GetGroupTextByKey(int grpKey, ref string text, ref string des)
  47. {
  48. text = resGroup[grpKey].text;
  49. des = resGroup[grpKey].Description;
  50. }
  51. public void SetStringByKey(int grpkey, int itmkey, string value, string des)
  52. {
  53. StringRes sr = new StringRes();
  54. sr.text = value;
  55. sr.Description = des;
  56. resGroup[grpkey].mapRes[itmkey] = sr;
  57. }
  58. public bool LoadStringFromXml()
  59. {
  60. XmlDocument xml = new XmlDocument();
  61. xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
  62. XmlNode root = xml.SelectSingleNode("Language");
  63. XmlNode root2 = root.SelectSingleNode("DefaultLanguage");
  64. string ss = root2.InnerText;
  65. XmlDocument doc1 = new XmlDocument();
  66. if (ss == "EN")
  67. {
  68. doc1.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForReport-EN.xml");//载入xml文件
  69. }
  70. else if (ss == "ZH")
  71. {
  72. doc1.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForReport-ZH.xml");//载入xml文件
  73. }
  74. root = doc1.SelectSingleNode("XMLData");
  75. root2 = root.SelectSingleNode("collection");
  76. XmlNodeList childlist = root2.ChildNodes;
  77. for (int i = 0; i < childlist.Count; i++)
  78. {
  79. int colkey = -1;
  80. if (childlist[i].Attributes["grpKey"] != null)
  81. {
  82. colkey = int.Parse(childlist[i].Attributes["grpKey"].Value);
  83. }
  84. string colText = "";
  85. if (childlist[i].Attributes["text"] != null)
  86. {
  87. colText = childlist[i].Attributes["text"].Value;
  88. }
  89. string colDes = "";
  90. if (childlist[i].Attributes["description"] != null)
  91. {
  92. colDes = childlist[i].Attributes["description"].Value;
  93. }
  94. ResGroup rg = new ResGroup();
  95. rg.key = colkey;
  96. rg.text = colText;
  97. rg.Description = colDes;
  98. XmlNodeList childlist2 = childlist[i].ChildNodes;
  99. for (int j = 0; j < childlist2.Count; j++)
  100. {
  101. StringRes sr = new StringRes();
  102. int key = int.Parse(childlist2[j].Attributes["itemKey"].Value);
  103. sr.key = key;
  104. sr.text = childlist2[j].Attributes["itemText"].Value;
  105. if (childlist2[j].Attributes["boDownList"] != null)
  106. {
  107. sr.DownList = childlist2[j].Attributes["boDownList"].Value;
  108. }
  109. else
  110. {
  111. sr.DownList = "空";
  112. }
  113. if (childlist2[j].Attributes["description"] != null)
  114. {
  115. sr.Description = childlist2[j].Attributes["description"].Value;
  116. }
  117. else
  118. {
  119. sr.Description = "空";
  120. }
  121. try
  122. {
  123. rg.mapRes[key] = sr;
  124. }
  125. catch (System.Exception ex)
  126. {
  127. // MessageBox.Show(ex.ToString());
  128. }
  129. }
  130. resGroup[colkey] = rg;
  131. }
  132. return true;
  133. }
  134. public static XmlResourceData GetInstance()
  135. {
  136. //static MultiLang instance;
  137. if (instance == null)
  138. {
  139. instance.LoadStringFromXml();
  140. }
  141. return instance;
  142. }
  143. }
  144. }