XmlResourceData.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\\ResourceForCpp\\ResourceForCpp-EN.xml");//载入xml文件
  66. }
  67. else if (ss == "ZH")
  68. {
  69. doc1.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-ZH.xml");//载入xml文件
  70. }
  71. root = doc1.SelectSingleNode("XMLData");
  72. root2 = root.SelectSingleNode("collection");
  73. XmlNodeList childlist = root2.ChildNodes;
  74. for (int i = 0; i < childlist.Count; i++)
  75. {
  76. int colkey = -1;
  77. if (childlist[i].Attributes["grpKey"] != null)
  78. {
  79. colkey = int.Parse(childlist[i].Attributes["grpKey"].Value);
  80. }
  81. string colText = "";
  82. if (childlist[i].Attributes["text"] != null)
  83. {
  84. colText = childlist[i].Attributes["text"].Value;
  85. }
  86. string colDes = "";
  87. if (childlist[i].Attributes["description"] != null)
  88. {
  89. colDes = childlist[i].Attributes["description"].Value;
  90. }
  91. ResGroup rg = new ResGroup();
  92. rg.key = colkey;
  93. rg.text = colText;
  94. rg.Description = colDes;
  95. XmlNodeList childlist2 = childlist[i].ChildNodes;
  96. for (int j = 0; j < childlist2.Count; j++)
  97. {
  98. StringRes sr = new StringRes();
  99. int key = int.Parse(childlist2[j].Attributes["itemKey"].Value);
  100. sr.key = key;
  101. sr.text = childlist2[j].Attributes["itemText"].Value;
  102. if (childlist2[j].Attributes["description"] != null)
  103. {
  104. sr.Description = childlist2[j].Attributes["description"].Value;
  105. }
  106. else
  107. {
  108. sr.Description = "空";
  109. }
  110. try
  111. {
  112. rg.mapRes[key] = sr;
  113. }
  114. catch (System.Exception ex)
  115. {
  116. // MessageBox.Show(ex.ToString());
  117. }
  118. }
  119. resGroup[colkey] = rg;
  120. }
  121. return true;
  122. }
  123. public static XmlResourceData GetInstance()
  124. {
  125. //static MultiLang instance;
  126. if (instance == null)
  127. {
  128. instance.LoadStringFromXml();
  129. }
  130. return instance;
  131. }
  132. }
  133. }