123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml;
- namespace OTSDataType
- {
- public struct StringRes
- {
- public int key;
- public string text;
- public string Description;
- }
- public class ResGroup
- {
- public int key;
- public string text = "";
- public string Description = "";
- public SortedDictionary<int, StringRes> mapRes = new SortedDictionary<int, StringRes>();
- }
- public class XmlResourceData
- {
- static XmlResourceData instance =new XmlResourceData();
- private SortedDictionary<int, ResGroup> resGroup = new SortedDictionary<int, ResGroup>();
- public XmlResourceData()
- {
- LoadStringFromXml();
- }
- public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des)
- {
- if (!resGroup.ContainsKey(grpKey))
- {
- return;
- }
- if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
- {
- return;
- }
- text = resGroup[grpKey].mapRes[itmKey].text;
- des = resGroup[grpKey].mapRes[itmKey].Description;
- }
- public string GetStringByKey(int grpKey, int itmKey)
- {
- if (!resGroup.ContainsKey(grpKey))
- {
- return "";
- }
- if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
- {
- return "";
- }
- return resGroup[grpKey].mapRes[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].mapRes.ContainsKey(itmKey))
- {
- return;
- }
- StringRes sr = new StringRes();
- sr.text = value;
- sr.Description = des;
- resGroup[grpKey].mapRes[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");
- //XmlDocument doc2 = new XmlDocument();
- //if (ss == "EN")
- //{
- // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-EN.xml");//载入xml文件
- //}
- //else if (ss == "ZH")
- //{
- // doc2.Load(".\\Resources\\XMLData\\ResourceForCpp\\ResourceForCpp-ZH.xml");//载入xml文件
- //}
- //XmlNode root1 = doc1.DocumentElement;
- ////合并两个Xml文档中的collection节点内容
- //root = doc2.SelectSingleNode("XMLData");
- //XmlNode xnl = root.SelectSingleNode("collection");
- //foreach (XmlNode xnItem in xnl)
- //{
- // XmlNode rootTemp = doc1.ImportNode(xnItem, true);
- // root2.AppendChild(rootTemp);
- //}
- 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;
- }
- ResGroup rg = new ResGroup();
- rg.key = colkey;
- rg.text = colText;
- rg.Description = colDes;
- XmlNodeList childlist2 = childlist[i].ChildNodes;
- for (int j = 0; j < childlist2.Count; j++)
- {
- StringRes sr = new StringRes();
- 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;
- }
- else
- {
- sr.Description = "空";
- }
- try
- {
- rg.mapRes[key] = sr;
- }
- catch (System.Exception ex)
- {
- // MessageBox.Show(ex.ToString());
- }
- }
- resGroup[colkey] = rg;
- }
- return true;
- }
- //public bool SaveStringToXml()
- //{
- // if (File.Exists(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml"))
- // {
- // XDocument xdoc = XDocument.Load(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
- // IEnumerable<XElement> elements = from ele in xdoc.Descendants("collection") select ele;
- // foreach (var ele in elements)
- // {
- // if (ele != null)
- // {
- // ele.RemoveAll();
- // }
- // }
- // IEnumerable<XElement> cls = from ele in xdoc.Descendants("collection") select ele;
- // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
- // {
- // XElement EleName = new XElement("member");
- // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
- // EleName.SetAttributeValue("itemName", "");
- // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
- // cls.First().Add(EleName);
- // }
- // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
- // }
- // else
- // {
- // XDocument xdoc = new XDocument();
- // XElement root = new XElement("XMLData");
- // XElement cls = new XElement("collection");
- // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
- // {
- // XElement EleName = new XElement("member");
- // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
- // EleName.SetAttributeValue("itemName", "");
- // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
- // cls.Add(EleName);
- // }
- // root.Add(cls);
- // xdoc.Add(root);
- // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
- // }
- // return true;
- //}
- public static XmlResourceData GetInstance()
- {
-
-
- return instance;
- }
- }
- }
|