`
lolocomee
  • 浏览: 16305 次
  • 性别: 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;
        }
    }
 
分享到:
评论

相关推荐

    adm-3058ah分布式采集模块 C#程序

    综上所述,"adm-3058ah分布式采集模块 C#程序"涉及到的知识点涵盖了硬件接口、数据处理、多线程编程、异步操作、异常处理、UI设计、数据库集成等多个方面,这些都是构建高效、可靠的分布式采集系统的关键技术。

    多线程采集C#源代码

    7. **数据采集**:这部分代码会根据实际需求,可能涉及到HTTP请求、网页解析(如HTML或XML)、数据库操作等,可能用到`HttpClient`、`WebClient`或第三方库如HtmlAgilityPack。 8. **日志记录**:为了调试和问题...

    C#实时股票分析源码

    C#开发的一款股票实时查看工具,主要是用C#编写,用到了WebService,又结合了XML技术,自认为还不错,如果有高手的话,可以改进成不使用WebService的,期待高手的加入。另外,这款小工具还可以自定义买股成本价和...

    C#Winform实例经典源码.rar

    7. **文件操作**:C#提供了System.IO命名空间,包含许多类(如FileStream、StreamReader、StreamWriter等)用于读写文件,方便在Winform应用中进行文件操作。 8. **对话框**:Winform内置了多种对话框,如...

    风车通用Web采集程序在线版_dotnet整站程序.rar

    4. **异步编程**:为了提高效率,Web采集程序通常采用异步编程模型,利用async/await关键字来避免阻塞主线程,实现多任务并行处理。 5. **数据存储**:采集到的数据可能需要保存到本地文件、数据库或者云存储。例如...

    C# 视频会议系统

    C#是一种面向对象的编程语言,由微软公司开发,广泛应用于Windows平台的软件开发,包括网络应用程序、桌面应用程序以及移动应用程序等。视频会议系统的开发是C#在现代通信技术中的实际应用之一。 【描述】:“通过...

    alexa自动采集

    在IT行业中,自动采集是一项常见的任务,特别是在数据分析和网络监控领域。本项目专注于"alexa自动采集",目的是从Alexa官网获取相关网站的数据,包括昨日排名、七天排名、综合排名、一月排名以及中国地区的排名。...

    C# 电源电压电流上位机

    10. 设备驱动程序接口(API):如果电源设备提供了专用的驱动程序或API,C#应用程序需要调用这些接口来进行数据采集和控制。 11. 安全性和稳定性:在设计软件时,必须考虑电源设备的安全性,避免错误操作导致设备...

    BK信息采集系统(源码版.net2.0改进版)

    在.NET 2.0环境下开发的BK信息采集系统可能利用了C#或VB.NET语言,这两种语言都是.NET Framework的重要组成部分,具有强大的编程能力,尤其是处理网络请求和解析HTML或XML文档时。 该系统可能包含以下关键组件和...

    Spider Website Data 网站数据采集器.zip

    【标题】"Spider Website Data 网站数据采集器.zip" 涉及的主要知识点是网站数据抓取和C#编程技术。这个压缩包中包含的项目源码可能是一个使用C#语言编写的网络爬虫系统,用于自动从网站上采集和处理数据。 **一、...

    上位机源码,上位机源码是什么,C#源码.zip.zip

    上位机通过各种通讯协议与下位机建立连接,实现对下位机的监控、配置、数据采集等功能。在工业自动化、物联网、智能家居等领域,上位机源码扮演着关键角色。 C#是一种面向对象的编程语言,由微软公司开发,用于构建...

    基于XMPP协议的视频会议系统C#源码

    - **Jabber组件**:XMPP协议的实现通常会用到Jabber组件,比如在C#中常用的Xmpp.Net库,用于创建XMPP客户端和服务器。 - **音视频编解码**:系统可能使用H.264/AVC进行视频编码,AAC或Opus进行音频编码,这些都...

    C#摄像头编程 代码简单

    在C#编程中,利用摄像头进行视频采集和处理是一项常见的任务,尤其在开发桌面应用、监控系统或视频会议软件时。本教程将深入探讨如何在C#环境下进行摄像头编程,以便实现摄像头的实时预览、录像、拍照等功能。 首先...

    qq2013 模仿qq 实现大部分功能

    4. **XML或JSON数据解析**:QQ的数据传输通常采用XML或JSON格式,方便服务器与客户端之间的数据交换。C#提供了内置的XmlDocument、Json.NET等库来处理这两种格式的数据。 5. **加密解密**:考虑到用户隐私和安全,...

    asp.net物联网后台管理系统源码.zip

    ASP.NET是微软提供的一个用于构建Web应用程序的开发平台,它集成了开发、调试和部署的全过程,支持多种编程语言,如C#、VB.NET等。在这个源码中,开发者可能使用了ASP.NET MVC(Model-View-Controller)架构,这是一...

    First-Project-C-:创建监控器应用

    2. **定时任务**:监控器应用需要定期收集数据,这需要用到定时器(Timer)类,通过设置间隔时间来触发事件,执行数据采集任务。 3. **系统信息访问**:C#提供了System.Diagnostics命名空间,其中的Process、...

    任务二数据端数据demo.zip_.net 监测_12_data_串口_数据监测

    1. **.NET Framework**: 这是一个由微软开发的通用软件框架,提供了丰富的类库和运行环境,支持多种编程语言,如C#、VB.NET等,用于构建串口通信应用程序。 2. **SerialPort 类**: .NET Framework 提供的 `System....

    用电计费系统 用电的困扰

    1. **数据采集模块**:通过连接到电表,实时读取用户的用电数据。这可能涉及到与不同类型的智能电表进行通信,如RS485接口或无线通信技术。 2. **计费算法模块**:根据各地电价和用电时段设定,计算每个用户应缴纳...

    BrainCap_Git

    在C#编程中,我们通常会用到.NET Framework或.NET Core作为开发平台,这两个平台提供了丰富的类库和工具支持,便于构建各种类型的应用程序,包括科学计算、数据分析和可视化等。对于脑电图数据处理,开发者可能使用...

Global site tag (gtag.js) - Google Analytics