`
lolocomee
  • 浏览: 15995 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

C# 采集4-程序用到的xml操作

阅读更多

程序用到的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;
        }
    }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics