1、创建一个类:XmlHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Windows.Forms; namespace WindowsFormsApplication14 { class XmlHelper { public XmlHelper() { } /// <summary> /// 读取数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// <returns>string</returns> /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") ************************************************/ public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// <summary> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") ************************************************/ public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { } } /// <summary> /// 修改数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch { } } /// <summary> /// 删除数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; //if (attribute.Equals("")) // xn.ParentNode.RemoveChild(xn); //else // xe.RemoveAttribute(attribute); xn.RemoveAll(); doc.Save(path); } catch { } } } }
2、窗体单击按钮调用代码
private void button1_Click(object sender, EventArgs e) { //删除元素 XmlHelper.Delete("F:\\XmlFile.xml", "/Root", ""); //表名 XmlHelper.Insert("F:\\XmlFile.xml", "/Root", "Table", "name", "m_C_User"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table[@name='m_C_User']", "", "TableEqualsForm", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table[@TableEqualsForm='False']", "", "title", "用户报表"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table[@title='用户报表']", "", "pageDown", "森林海科技有限公司"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table", "Items", "", ""); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "UserNo"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='UserNo']", "", "Text", "用户编号"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='用户编号']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='UserNo'and @Text='用户编号'and @width='20']", "", "summary", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "UserName"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='UserName']", "", "Text", "用户姓名"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='用户姓名']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='UserName'and @Text='用户姓名'and @width='20']", "", "summary", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "Sex"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='Sex']", "", "Text", "性别"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='性别']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='Sex'and @Text='性别'and @width='20']", "", "summary", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "TelNum"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='TelNum']", "", "Text", "联系电话"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='联系电话']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='TelNum'and @Text='联系电话'and @width='20']", "", "summary", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "DeptName"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='DeptName']", "", "Text", "部门"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='部门']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='DeptName'and @Text='部门'and @width='20']", "", "summary", "False"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items", "Item", "name", "Headship"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='Headship']", "", "Text", "职位"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@Text='职位']", "", "width", "20"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/Table/Items/Item[@name='Headship'and @Text='职位'and @width='20']", "", "summary", "False"); //插入元素/属性 XmlHelper.Insert("F:\\XmlFile.xml", "/Root", "ReportFild", "name", "m_C_User"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild", "Items", "", ""); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "UserNo"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='UserNo']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='UserNo'and @value='True']", "", "width", "100"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "UserName"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='UserName']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='UserName'and @value='True']", "", "width", "100"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "Sex"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='Sex']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='Sex'and @value='True']", "", "width", "100"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "TelNum"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='TelNum']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='TelNum'and @value='True']", "", "width", "100"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "DeptName"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='DeptName']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='DeptName'and @value='True']", "", "width", "100"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items", "Item", "name", "Headship"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='Headship']", "", "value", "True"); XmlHelper.Insert("F:\\XmlFile.xml", "/Root/ReportFild/Items/Item[@name='Headship'and @value='True']", "", "width", "100"); }
3、XmlFile.xml写入结果:
<?xml version="1.0" encoding="utf-8"?> <Root> <Table name="m_C_User" TableEqualsForm="False" title="用户报表" pageDown="森林海科技有限公司"> <Items> <Item name="UserNo" Text="用户编号" width="20" summary="False" /> <Item name="UserName" Text="用户姓名" width="20" summary="False" /> <Item name="Sex" Text="性别" width="20" summary="False" /> <Item name="TelNum" Text="联系电话" width="20" summary="False" /> <Item name="DeptName" Text="部门" width="20" summary="False" /> <Item name="Headship" Text="职位" width="20" summary="False" /> </Items> </Table> <ReportFild name="m_C_User"> <Items> <Item name="UserNo" value="True" width="100" /> <Item name="UserName" value="True" width="100" /> <Item name="Sex" value="True" width="100" /> <Item name="TelNum" value="True" width="100" /> <Item name="DeptName" value="True" width="100" /> <Item name="Headship" value="True" width="100" /> </Items> </ReportFild> </Root>