C# winform窗体简单保存界面控件参数到xml
程序员文章站
2022-03-20 13:10:08
引用网上的 XMLHelper 地址 :https://www.cnblogs.com/chnboy/archive/2009/04/02/1427652.html 稍做修改 using System; using System.Linq; using System.Xml; namespace T ......
引用网上的 xmlhelper 地址 :
稍做修改
using system; using system.linq; using system.xml; namespace test { /// <summary> /// xmlhelper xml文档操作管理器 /// </summary> public class xmlhelper { public xmlhelper() { // // todo: 在此处添加构造函数逻辑 // } #region xml文档节点查询和读取 /// <summary> /// 选择匹配xpath表达式的第一个节点xmlnode. /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名")</param> /// <returns>返回xmlnode</returns> public static xmlnode getxmlnodebyxpath(string xmlfilename, string xpath) { xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); return xmlnode; } catch (exception ex) { return null; //throw ex; //这里可以定义你自己的异常处理 } } /// <summary> /// 选择匹配xpath表达式的节点列表xmlnodelist. /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名")</param> /// <returns>返回xmlnodelist</returns> public static xmlnodelist getxmlnodelistbyxpath(string xmlfilename, string xpath) { xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnodelist xmlnodelist = xmldoc.selectnodes(xpath); return xmlnodelist; } catch (exception ex) { return null; //throw ex; //这里可以定义你自己的异常处理 } } /// <summary> /// 选择匹配xpath表达式的第一个节点的匹配xmlattributename的属性xmlattribute. /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlattributename">要匹配xmlattributename的属性名称</param> /// <returns>返回xmlattributename</returns> public static xmlattribute getxmlattribute(string xmlfilename, string xpath, string xmlattributename) { string content = string.empty; xmldocument xmldoc = new xmldocument(); xmlattribute xmlattribute = null; try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { if (xmlnode.attributes.count > 0) { xmlattribute = xmlnode.attributes[xmlattributename]; } } } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return xmlattribute; } /// <summary> /// 选择匹配xpath表达式的第一个节点的匹配xmlattributename的属性xmlattribute. /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlattributename">要匹配xmlattributename的属性名称</param> /// <returns>返回xmlattributename</returns> public static string getxmlbyattribute(string xmlfilename, string xpath, string name, string xmlattributename, string xmlattributevalues) { string content = string.empty; xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 //主要 xmlnodelist xlist = xmldoc.selectnodes(xpath); xmlnode xmlnode = xlist.cast<xmlnode>().where(o => o.attributes.cast<xmlattribute>().firstordefault(a => a.name == xmlattributename && a.value == xmlattributevalues) != null).firstordefault(); //xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { foreach (xmlnode node in xmlnode.childnodes) { if (node.name.tolower() == name.tolower()) { //存在此节点则返回innertext content = node.innertext; break; } } } } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return content; } #endregion #region xml文档创建和节点或属性的添加、修改 /// <summary> /// 创建一个xml文档 /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="rootnodename">xml文档根节点名称(须指定一个根节点名称)</param> /// <param name="version">xml文档版本号(必须为:"1.0")</param> /// <param name="encoding">xml文档编码方式</param> /// <param name="standalone">该值必须是"yes"或"no",如果为null,save方法不在xml声明上写出独立属性</param> /// <returns>成功返回true,失败返回false</returns> public static bool createxmldocument(string xmlfilename, string rootnodename, string version, string encoding, string standalone) { bool issuccess = false; try { xmldocument xmldoc = new xmldocument(); xmldeclaration xmldeclaration = xmldoc.createxmldeclaration(version, encoding, standalone); xmlnode root = xmldoc.createelement(rootnodename); xmldoc.appendchild(xmldeclaration); xmldoc.appendchild(root); xmldoc.save(xmlfilename); issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 依据匹配xpath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点 /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlnodename">要匹配xmlnodename的节点名称</param> /// <param name="innertext">节点文本值</param> /// <param name="xmlattributename">要匹配xmlattributename的属性名称</param> /// <param name="value">属性值</param> /// <returns>成功返回true,失败返回false</returns> public static bool createxmlnodebyxpath(string xmlfilename, string xpath, string xmlnodename, string innertext, string xmlattributename, string value) { bool issuccess = false; xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //存不存在此节点都创建 xmlelement subelement = xmldoc.createelement(xmlnodename); subelement.innerxml = innertext; //如果属性和值参数都不为空则在此新节点上新增属性 if (!string.isnullorempty(xmlattributename) && !string.isnullorempty(value)) { xmlattribute xmlattribute = xmldoc.createattribute(xmlattributename); xmlattribute.value = value; subelement.attributes.append(xmlattribute); } xmlnode.appendchild(subelement); } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 依据匹配xpath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建) /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlnodename">要匹配xmlnodename的节点名称</param> /// <param name="innertext">节点文本值</param> /// <returns>成功返回true,失败返回false</returns> public static bool createorupdatexmlnodebyxpath(string xmlfilename, string xpath, string xmlnodename, string innertext) { bool issuccess = false; bool isexistsnode = false;//标识节点是否存在 xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 //xmlnodelist xlist = xmldoc.selectnodes(xpath); //xmlnode xmlnode = xlist.cast<xmlnode>().where(o => o.innertext == innertext).firstordefault(); xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //遍历xpath节点下的所有子节点 foreach (xmlnode node in xmlnode.childnodes) { //xmlattribute att = node.attributes.cast<xmlattribute>().where(o => o.name == innertext).firstordefault(); if (node.name.tolower() == xmlnodename.tolower()) { //存在此节点则更新 node.innerxml = innertext; isexistsnode = true; break; } } if (!isexistsnode) { //不存在此节点则创建 xmlelement subelement = xmldoc.createelement(xmlnodename); subelement.innerxml = innertext; xmlnode.appendchild(subelement); } } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 依据匹配xpath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建) /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlnodename">要匹配xmlnodename的节点名称</param> /// <param name="innertext">节点文本值</param> /// <returns>成功返回true,失败返回false</returns> public static bool createorupdatexmlnodebyxpath(string xmlfilename, string xpath, string xmlnodename, string innertext, string xmlattributename, string xmlattributevalues) { bool issuccess = false; bool isexistsnode = false;//标识节点是否存在 xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //遍历xpath节点下的所有子节点 foreach (xmlnode node in xmlnode.childnodes) { xmlattribute att = node.attributes.cast<xmlattribute>().where(o => o.name == xmlattributename && o.value == xmlattributevalues).firstordefault(); if (att != null && node.name.tolower() == xmlnodename.tolower()) { //存在此节点则更新 node.innerxml = innertext; isexistsnode = true; #region //遍历xpath节点中的所有属性 bool isexistsattribute = false; foreach (xmlattribute attribute in node.attributes) { if (attribute.name.tolower() == xmlattributename.tolower()) { //节点中存在此属性则更新 attribute.value = xmlattributevalues; isexistsattribute = true; break; } } if (!isexistsattribute) { //节点中不存在此属性则创建 xmlattribute xmlattribute = xmldoc.createattribute(xmlattributename); xmlattribute.value = xmlattributevalues; node.attributes.append(xmlattribute); } #endregion break; } } if (!isexistsnode) { //不存在此节点则创建 xmlelement subelement = xmldoc.createelement(xmlnodename); subelement.innerxml = innertext; xmlattribute xmlattribute = xmldoc.createattribute(xmlattributename); xmlattribute.value = xmlattributevalues; subelement.attributes.append(xmlattribute); xmlnode.appendchild(subelement); } } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 依据匹配xpath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建) /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlnodename">要匹配xmlnodename的节点名称</param> /// <param name="innertext">节点文本值</param> /// <param name="parentinnertext">指定父节点文本值</param> /// <returns>成功返回true,失败返回false</returns> public static bool createorupdatexmlnodebyxpath(string xmlfilename, string xpath, string xmlnodename, string innertext, string parentattributevalues) { bool issuccess = false; bool isexistsnode = false;//标识节点是否存在 xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnodelist xlist = xmldoc.selectnodes(xpath); //主要 xmlnode xmlnode = xlist.cast<xmlnode>().where(o => o.attributes.cast<xmlattribute>().firstordefault(a => a.name == "name" && a.value == parentattributevalues) != null).firstordefault(); //xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //遍历xpath节点下的所有子节点 foreach (xmlnode node in xmlnode.childnodes) { if (node.name.tolower() == xmlnodename.tolower()) { //存在此节点则更新 node.innerxml = innertext; isexistsnode = true; break; } } if (!isexistsnode) { //不存在此节点则创建 xmlelement subelement = xmldoc.createelement(xmlnodename); subelement.innerxml = innertext; xmlnode.appendchild(subelement); } } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 依据匹配xpath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建) /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlattributename">要匹配xmlattributename的属性名称</param> /// <param name="value">属性值</param> /// <returns>成功返回true,失败返回false</returns> public static bool createorupdatexmlattributebyxpath(string xmlfilename, string xpath, string xmlattributename, string value) { bool issuccess = false; bool isexistsattribute = false;//标识属性是否存在 xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //遍历xpath节点中的所有属性 foreach (xmlattribute attribute in xmlnode.attributes) { if (attribute.name.tolower() == xmlattributename.tolower()) { //节点中存在此属性则更新 attribute.value = value; isexistsattribute = true; break; } } if (!isexistsattribute) { //节点中不存在此属性则创建 xmlattribute xmlattribute = xmldoc.createattribute(xmlattributename); xmlattribute.value = value; xmlnode.attributes.append(xmlattribute); } } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } #endregion #region xml文档节点或属性的删除 /// <summary> /// 删除匹配xpath表达式的第一个节点(节点中的子元素同时会被删除) /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <returns>成功返回true,失败返回false</returns> public static bool deletexmlnodebyxpath(string xmlfilename, string xpath) { bool issuccess = false; xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //删除节点 xmlnode.parentnode.removechild(xmlnode); } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 删除匹配xpath表达式的第一个节点中的匹配参数xmlattributename的属性 /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlattributename">要删除的xmlattributename的属性名称</param> /// <returns>成功返回true,失败返回false</returns> public static bool deletexmlattributebyxpath(string xmlfilename, string xpath, string xmlattributename) { bool issuccess = false; bool isexistsattribute = false; xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); xmlattribute xmlattribute = null; if (xmlnode != null) { //遍历xpath节点中的所有属性 foreach (xmlattribute attribute in xmlnode.attributes) { if (attribute.name.tolower() == xmlattributename.tolower()) { //节点中存在此属性 xmlattribute = attribute; isexistsattribute = true; break; } } if (isexistsattribute) { //删除节点中的属性 xmlnode.attributes.remove(xmlattribute); } } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } /// <summary> /// 删除匹配xpath表达式的第一个节点中的所有属性 /// </summary> /// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param> /// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param> /// <returns>成功返回true,失败返回false</returns> public static bool deleteallxmlattributebyxpath(string xmlfilename, string xpath) { bool issuccess = false; xmldocument xmldoc = new xmldocument(); try { xmldoc.load(xmlfilename); //加载xml文档 xmlnode xmlnode = xmldoc.selectsinglenode(xpath); if (xmlnode != null) { //遍历xpath节点中的所有属性 xmlnode.attributes.removeall(); } xmldoc.save(xmlfilename); //保存到xml文档 issuccess = true; } catch (exception ex) { throw ex; //这里可以定义你自己的异常处理 } return issuccess; } #endregion } }
添加 formhelper
using system; using system.windows.forms; namespace test { public class formhelper { //这是xml文档物理文件名(包含物理路径) private static string xmlfilename = application.startuppath + @"\config\parameter.xml"; //这是xml文档根节点名 private static string rootnodename = "forms"; /// <summary> /// 保存控件参数 /// 放到formclosing事件里 /// </summary> /// <param name="c"></param> public static void setformparam(control c) { creatxml(); string str = ""; getparam(c, ref str); str = str.trimend('|'); //保存xml文档 string xpath = "/" + rootnodename; //这是新节点的父节点路径/forms string nodename = "formname"; //这是新节点名称,在父节点下新增 string nodetext = c.text;//窗体名 bool issuccess = xmlhelper.createorupdatexmlnodebyxpath(xmlfilename, xpath, nodename, "", "name", nodetext); string xpath2 = xpath + "/" + nodename; //这是新子节点的父节点路径/forms/formname string nodename2 = "parm"; //这是新子节点名称,在父节点下新增 string nodetext2 = str;//窗体控件参数字符串 issuccess = xmlhelper.createorupdatexmlnodebyxpath(xmlfilename, xpath2, nodename2, nodetext2, nodetext); } /// <summary> /// 加载控件参数 /// 放到load事件尾 /// </summary> /// <param name="c"></param> public static void loadformparam(control c) { //获取xml参数文档 if (system.io.file.exists(xmlfilename)) { string xpath = "/" + rootnodename + "/formname"; string parm = xmlhelper.getxmlbyattribute(xmlfilename, xpath, "parm", "name", c.text); setparam(c, parm); } } private static void creatxml() { if (!system.io.file.exists(xmlfilename)) { if (!system.io.directory.exists(system.io.path.getdirectoryname(xmlfilename))) system.io.directory.createdirectory(system.io.path.getdirectoryname(xmlfilename)); xmlhelper.createxmldocument(xmlfilename, rootnodename, "1.0", "utf-8", null); //messagebox.show("xml文档创建成功:" + xmlfilename); } } private static void getparam(control c, ref string str) { try { foreach (control ctl in c.controls) { if (ctl is textbox) { textbox tx = ctl as textbox; str += tx.name + "$" + tx.text + "|"; } if (ctl is checkbox) { checkbox tx = ctl as checkbox; str += tx.name + "$" + tx.checked + "|"; } if (ctl is combobox) { combobox tx = ctl as combobox; str += tx.name + "$" + tx.selectedindex.tostring() + "|"; } if (ctl.controls.count > 0) getparam(ctl, ref str); } } catch (exception) { } } private static void setparam(control c, string parm) { try { foreach (control ctl in c.controls) { foreach (string str in parm.split('|')) { if (ctl.name == str.split('$')[0]) { if (ctl is textbox) { textbox tx = ctl as textbox; tx.text = str.split('$')[1]; } if (ctl is checkbox) { checkbox tx = ctl as checkbox; tx.checked = convert.toboolean(str.split('$')[1]); } if (ctl is combobox) { combobox tx = ctl as combobox; tx.selectedindex = convert.toint32(str.split('$')[1]); } } } if (ctl.controls.count > 0) setparam(ctl, parm); } } catch (exception) { } } } }
界面调用
保存参数
private void form1_formclosing(object sender, formclosingeventargs e) { formhelper.setformparam(this); }
回读参数
private void form1_load(object sender, eventargs e) { formhelper.loadformparam(this); }
xml 文档
<?xml version="1.0" encoding="utf-8"?> <forms> <formname name="测试"> <parm>combobox1$2|checkbox1$true|textbox3$pantext|textbox2$sfafas|textbox1$123</parm> </formname> <formname name="form1"> <parm>textbox3$pantext|textbox2$sfafas|textbox1$123</parm> </formname> </forms>
测试界面
只做了textbox、checkbox、combobox参数保存 如需增加自行修改formhelper里的getparam和setparam方法
上一篇: 禽兽啊,没想到宅男口味这么重!
下一篇: 布局技巧:使用ViewStub