using OTSModelSharp.ResourceManage; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Xml; namespace OTSDataType { public class StringResource { public int key; public string text; public List combolist=new List(); public string Description; public string Tag; } public class ResourceGroup { public int key; public string text = ""; public string Description = ""; public SortedDictionary resourceDic = new SortedDictionary(); } public class XmlResourceData { static XmlResourceData instance =new XmlResourceData(); private SortedDictionary resGroup = new SortedDictionary(); public XmlResourceData() { LoadStringFromXml(); } public StringResource GetStringResourceByKey(OTS_SAMPLE_PROP_GRID_ITEM_GROUPS grpKey, OTS_SAMPLE_PROP_GRID_ITEMS itmKey) { StringResource re = new StringResource(); if (!resGroup.ContainsKey((int)grpKey)) { NLog.LogManager.GetCurrentClassLogger().Warn("Cann't find the string resource group number " + grpKey.ToString()); return re; } if (!resGroup[(int)grpKey].resourceDic.ContainsKey((int)itmKey)) { NLog.LogManager.GetCurrentClassLogger().Warn("Cann't find the string resource group number :" + grpKey.ToString() + "item number:" + itmKey.ToString()); ; return re; } return resGroup[(int)grpKey].resourceDic[(int)itmKey]; } public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des) { if (!resGroup.ContainsKey(grpKey)) { return; } if (!resGroup[grpKey].resourceDic.ContainsKey(itmKey)) { return; } text = resGroup[grpKey].resourceDic[itmKey].text; des = resGroup[grpKey].resourceDic[itmKey].Description; } public string GetStringByKey(int grpKey, int itmKey) { if (!resGroup.ContainsKey(grpKey)) { return ""; } if (!resGroup[grpKey].resourceDic.ContainsKey(itmKey)) { return ""; } return resGroup[grpKey].resourceDic[itmKey].text; } public string GetGroupTextByKey(int grpKey) { if (!resGroup.ContainsKey(grpKey)) { return ""; } return resGroup[grpKey].text; } public void GetGroupTextByKey(int grpKey, ref string text, ref string des) { if (!resGroup.ContainsKey(grpKey)) { return ; } text = resGroup[grpKey].text; des = resGroup[grpKey].Description; } public void SetStringByKey(int grpKey, int itmKey, string value, string des) { if (!resGroup.ContainsKey(grpKey)) { return; } if (!resGroup[grpKey].resourceDic.ContainsKey(itmKey)) { return; } StringResource sr = new StringResource(); sr.text = value; sr.Description = des; resGroup[grpKey].resourceDic[itmKey] = sr; } public bool LoadStringFromXml() { XmlDocument xml = new XmlDocument(); string path= Application.StartupPath + @"\Resources\XMLData\LanguageDefine.xml"; if (!File.Exists(path)) { NLog.LogManager.GetCurrentClassLogger().Error("There's no \\Resources\\XMLData\\LanguageDefine.xml"); return false; } xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml"); XmlNode root = xml.SelectSingleNode("Language"); XmlNode root2 = root.SelectSingleNode("DefaultLanguage"); string ss = root2.InnerText; XmlDocument doc1 = new XmlDocument(); if (ss == "EN") { doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-EN.xml");//载入xml文件 } else if (ss == "ZH") { doc1.Load(".\\Resources\\XMLData\\ResourceForMeasureSourceGrid-ZH.xml");//载入xml文件 } root = doc1.SelectSingleNode("XMLData"); root2 = root.SelectSingleNode("collection"); XmlNodeList childlist = root2.ChildNodes; for (int i = 0; i < childlist.Count; i++) { int colkey = -1; if (childlist[i].Attributes["grpKey"] != null) { colkey = int.Parse(childlist[i].Attributes["grpKey"].Value); } string colText = ""; if (childlist[i].Attributes["text"] != null) { colText = childlist[i].Attributes["text"].Value; } string colDes = ""; if (childlist[i].Attributes["description"] != null) { colDes = childlist[i].Attributes["description"].Value; } ResourceGroup rg = new ResourceGroup(); rg.key = colkey; rg.text = colText; rg.Description = colDes; XmlNodeList childlist2 = childlist[i].ChildNodes; for (int j = 0; j < childlist2.Count; j++) { StringResource sr = new StringResource(); int key = int.Parse(childlist2[j].Attributes["itemKey"].Value); sr.key = key; sr.text = childlist2[j].Attributes["itemText"].Value; if (childlist2[j].Attributes["description"] != null) { sr.Description = childlist2[j].Attributes["description"].Value; } if (childlist2[j].Attributes["comboContent"] != null) { string content= childlist2[j].Attributes["comboContent"].Value; var combodata = content.Split(','); sr.combolist.AddRange(combodata); } rg.resourceDic[key] = sr; } resGroup[colkey] = rg; } return true; } public static XmlResourceData GetInstance() { return instance; } } }