public void Save(string filePath) { if (!File.Exists(filePath)) //文件不存在 { CreateXmlFile(filePath); } XmlDocument xd = new XmlDocument(); XmlElement xeRoot = xd.CreateElement("root"); xd.AppendChild(xeRoot); string xmlTxt, typeTxt; JFFunctions.ToXTString(dictNamesInTag, out xmlTxt, out typeTxt); XmlElement xeNamesInTag = xd.CreateElement("Tag-Names"); xeRoot.AppendChild(xeNamesInTag); xeNamesInTag.InnerText = xmlTxt; JFFunctions.ToXTString(dictNameValue, out xmlTxt, out typeTxt); XmlElement xeItems = xd.CreateElement("Items"); xeRoot.AppendChild(xeItems); xeItems.InnerText = xmlTxt; xd.Save(filePath); }
/// <summary> /// 从文件中加载参数配置 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="isOpenOrCreate">文件不存在时是否创建新文件,=True:创建新文件 ; =False:不创建新文件,会抛出一个异常</param> public void Load(string filePath, bool isOpenOrCreate) { if (!File.Exists(filePath)) //文件不存在 { if (!isOpenOrCreate) { throw new FileNotFoundException(string.Format("JFXCfg.Load(filePath={0},isOpenOrCreate={1}) failed by: FilePath is Nonexists!", filePath, isOpenOrCreate)); } CreateXmlFile(filePath); dictNamesInTag.Clear(); dictNameValue.Clear(); FilePath = filePath; return; } XmlDocument xd = new XmlDocument(); xd.Load(filePath); XmlNode xnRoot = xd.SelectSingleNode("root"); XmlElement xeTagNames = xnRoot.SelectSingleNode("Tag-Names") as XmlElement; if (null != xeTagNames && !string.IsNullOrEmpty(xeTagNames.InnerText)) { dictNamesInTag = JFFunctions.FromXTString(xeTagNames.InnerText, typeof(JFXmlSortedDictionary <string, List <string> >).AssemblyQualifiedName) as JFXmlSortedDictionary <string, List <string> >; } else { dictNamesInTag.Clear(); } XmlElement xeItems = xnRoot.SelectSingleNode("Items") as XmlElement; if (null != xeItems && !string.IsNullOrEmpty(xeItems.InnerText)) { dictNameValue = JFFunctions.FromXTString(xeItems.InnerText, typeof(JFXmlDictionary <string, object>).AssemblyQualifiedName) as JFXmlDictionary <string, object>; } else { dictNameValue.Clear(); } //JFXCfg cfg = this.ReadXmlFile(filePath); //dictNamesInTag = cfg.dictNamesInTag; //dictNameValue = cfg.dictNameValue; //cfg = null; FilePath = filePath; }