程序用到的xml操作,主要是与任务相关,所以放到同个命名空间中。这个可要自己敲了,高兴。
xml文件格式:
<?xml version="1.0" encoding="utf-8"?> <setting> <datasource>/data.mdb</datasource> <tasks> <task name="" ecode="utf-8" state="off"> <list url="" category="" listareaf="" listareae="" rextitle="" /> <content contentf="" contente="" articlef="" articlee="" authorf="" authore="" sourcef="" sourcee="" datef="" datee="" /> </task> </tasks> </setting>
//xml操作 class XmlDao { /// <summary> /// 插入一个task /// </summary> /// <param name="t"></param> /// <param name="xmlDoc"></param> /// <param name="path"></param> public void addTask(Task t, XmlDocument xmlDoc,string path) { XmlNode rot = xmlDoc.SelectSingleNode("setting");//查找<tasks> XmlNode root = rot.SelectSingleNode("tasks");//查找<tasks> XmlElement xe1 = xmlDoc.CreateElement("task");//创建一个<task>节点 xe1.SetAttribute("name", t.Name);//设置该节点name属性 xe1.SetAttribute("ecode", t.Ecode);//设置该节点ecode属性 xe1.SetAttribute("state", t.State);//设置该节点ecode属性 XmlElement xesub1 = xmlDoc.CreateElement("list"); xesub1.SetAttribute("url",t.Url); xesub1.SetAttribute("category", t.Category); xesub1.SetAttribute("listareaf", t.ListAreaf); xesub1.SetAttribute("listareae", t.ListAreae); xesub1.SetAttribute("rextitle", t.RexTitle); xe1.AppendChild(xesub1); XmlElement xesub2 = xmlDoc.CreateElement("content"); xesub2.SetAttribute("contentf", t.Contentf); xesub2.SetAttribute("contente", t.Contente); xesub2.SetAttribute("articlef", t.Articlef); xesub2.SetAttribute("articlee", t.Articlee); xesub2.SetAttribute("authorf", t.Authorf); xesub2.SetAttribute("authore", t.Authore); xesub2.SetAttribute("sourcef", t.Sourcef); xesub2.SetAttribute("sourcee", t.Sourcee); xesub2.SetAttribute("datef", t.Datef); xesub2.SetAttribute("datee", t.Datee); xe1.AppendChild(xesub2); root.AppendChild(xe1);//添加到<tasks>节点中 xmlDoc.Save(path); } /// <summary> /// 修改单个任务 /// </summary> /// <param name="old"></param> /// <param name="t"></param> /// <param name="xmlDoc"></param> /// <param name="path"></param> public void editTask(Task old,Task t, XmlDocument xmlDoc,string path) { XmlNode root = xmlDoc.SelectSingleNode("setting"); XmlNodeList nodeList = root.SelectSingleNode("tasks").ChildNodes;//获取tasks节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("name") == old.Name)//如果name属性为要修改 { xe.SetAttribute("name", t.Name);//设置该节点name属性 xe.SetAttribute("ecode", t.Ecode);//设置该节点ecode属性 xe.SetAttribute("state", t.State);//设置该节点ecode属性 XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历 { XmlElement xe2 = (XmlElement)xn1;//转换类型 if (xe2.Name == "list") { xe2.SetAttribute("url", t.Url); xe2.SetAttribute("category", t.Category); xe2.SetAttribute("listareaf", t.ListAreaf); xe2.SetAttribute("listareae", t.ListAreae); xe2.SetAttribute("rextitle", t.RexTitle); } if (xe2.Name == "content") { xe2.SetAttribute("contentf", t.Contentf); xe2.SetAttribute("contente", t.Contente); xe2.SetAttribute("articlef", t.Articlef); xe2.SetAttribute("articlee", t.Articlee); xe2.SetAttribute("authorf", t.Authorf); xe2.SetAttribute("authore", t.Authore); xe2.SetAttribute("sourcef", t.Sourcef); xe2.SetAttribute("sourcee", t.Sourcee); xe2.SetAttribute("datef", t.Datef); xe2.SetAttribute("datee", t.Datee); } } break;//跳出foreach } } xmlDoc.Save(path);//保存 } /// <summary> /// 修改任务状态 /// </summary> /// <param name="xmlDoc"></param> /// <param name="path"></param> /// <param name="name"></param> public void editState(XmlDocument xmlDoc, string path, string name) { XmlNode root = xmlDoc.SelectSingleNode("setting"); XmlNodeList nodeList = root.SelectSingleNode("tasks").ChildNodes;//获取tasks节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("name") == name)//如果name属性为要修改 { string state = xe.GetAttribute("state"); xe.SetAttribute("state", state == "on" ? "off" : "on"); break;//跳出foreach } } xmlDoc.Save(path); } /// <summary> /// 获取任务列表 /// </summary> /// <param name="state"></param> /// <param name="xmlDoc"></param> /// <returns></returns> public List<Task> getTasks(string state, XmlDocument xmlDoc) { List<Task> Tlist = new List<Task>(); XmlNode root = xmlDoc.SelectSingleNode("setting"); XmlNodeList nodeList = root.SelectSingleNode("tasks").ChildNodes; if (state == "") { foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.Name == "task") { Task t = new Task(); t = getFromXml(xe, t); Tlist.Add(t); } } } else { foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.Name == "task" && state == xe.GetAttribute("state")) { Task t = new Task(); t = getFromXml(xe, t); Tlist.Add(t); } } } return Tlist; } private Task getFromXml(XmlElement xe,Task t) { t.Name = xe.GetAttribute("name"); t.Ecode = xe.GetAttribute("ecode"); t.State = xe.GetAttribute("state"); XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历 { XmlElement xe2 = (XmlElement)xn1;//转换类型 if (xe2.Name == "list") { t.Url = xe2.GetAttribute("url"); t.Category = xe2.GetAttribute("category"); t.ListAreaf = xe2.GetAttribute("listareaf"); t.ListAreae = xe2.GetAttribute("listareae"); t.RexTitle = xe2.GetAttribute("rextitle"); } if (xe2.Name == "content") { t.Contentf = xe2.GetAttribute("contentf"); t.Contente = xe2.GetAttribute("contente"); t.Articlef = xe2.GetAttribute("articlef"); t.Articlee = xe2.GetAttribute("articlee"); t.Authorf = xe2.GetAttribute("authorf"); t.Authore = xe2.GetAttribute("authore"); t.Sourcef = xe2.GetAttribute("sourcef"); t.Sourcee = xe2.GetAttribute("sourcee"); t.Datef = xe2.GetAttribute("datef"); t.Datee = xe2.GetAttribute("datee"); } } return t; } /// <summary> /// 删除name节点 /// </summary> /// <param name="name"></param> /// <param name="xmlDoc"></param> /// <param name="path"></param> public void delete(string name, XmlDocument xmlDoc,string path) { XmlNode root = xmlDoc.SelectSingleNode("setting"); XmlNode rot=root.SelectSingleNode("tasks"); XmlNodeList nodeList = rot.ChildNodes;//获取tasks节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("name") == name)//如果name属性为要修改 { rot.RemoveChild(xn); break;//跳出foreach } } xmlDoc.Save(path); } /// <summary> /// 获取单个任务 /// </summary> /// <param name="name"></param> /// <returns></returns> public Task getOne(string name, XmlDocument xmlDoc) { Task t = new Task(); XmlNode root = xmlDoc.SelectSingleNode("setting"); XmlNodeList nodeList = root.SelectSingleNode("tasks").ChildNodes;//获取tasks节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("name") == name)//如果name属性为要修改 { t = getFromXml(xe, t); break;//跳出foreach } } return t; } }
相关推荐
中国XML论坛--遍历XML代码(C#).htm中国XML论坛--遍历XML代码(C#).htm中国XML论坛--遍历XML代码(C#).htm中国XML论坛--遍历XML代码(C#).htm中国XML论坛--遍历XML代码(C#).htm中国XML论坛--遍历XML代码(C#)...
该资源采用C#技术 主要操作为用C#技术修改XML文件 便于保存和阅读 代码简单易懂 方便大家学习
该资源的主要技术 就是简单C#操作 目的为从C#操作中生成XML文件 这种技术方便大家保存文件 且易于切换 保存的XML文件可在别处使用 代码简单易懂 方便学习
C# 生成xml文件,编码为utf-8方法
数据库与XML结合--微软XML的读取 该资源附带数据库和C#源代码 主要操作为:从xml中读取数据 然后再把数据插入数据库 代码简易易懂 方便需求者参考
飞悦580·制作 <br>解压密码:www.fy580.cn<br><br>北大青鸟ACCP5.0二期 --- <br>深入.NET平台和C#编程 --- <br>项目实战 --- 《影院售票系统》 <br> <br>ShowList.xml --- XML文件 <br>影院...
C#操作xml方法-----C#.Net实例代码
网络下载图片程序,从电影院下载影院介绍等信息,并过滤影院电话地址经纬度等信息。可在xml配置文件中配置需下载图片的网站地址。
C#中操作XML实例,读取XML中的数据载入DataGridView中,实现对XML的增删改查操作。 visual studio 2005编译。
C#权限管理例子,提示包括XML的各种操作。
★☆★☆飞飞大叔荣誉出品★☆★☆ <br>C#版E-Main采集程序,带源码(有注释) 带正则表达式,想学习表达式的同志努力咯.. XP - VS2005 下编译通过``还添加邮件发送功能,只需要打开IIS 邮件服务器就可``` <br>★...
C#操作XML文件完整源码(十分经典)
XML操作类-C# protected XmlDocument objXml = new XmlDocument()
最近做项目,要用C#写,而且数据格式是那种节点套节点能把人套死的XML,看着都觉得晕,而且节点跳跃性很大,真心觉得单纯用环境自带的XMLDocument类完全达不到自己的需要,因为XML里面涉及到节点循环,循环里面又套...
c#自动采集程序源代码
NI数据采集卡,C#数据采集程序。。。。
C#源代码-,有注释
C#操作XML实例
C#-Xml操作类(序列化,格式化)
C#源代码-,有注释