热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

XML操作的再次封装,制作简化XML文件操作步骤的控件。

最近发现操作XML比较多,并且操作方式都差不多,也就是读、编、删的问题,所以对XML操作再次封装,做了一个Controlle

最近发现操作XML比较多,并且操作方式都差不多,也就是读、编、删的问题,所以对XML操作再次封装,做了一个ControllerXML.cs.dll,这样一来下次引入DLL,就可以省略去很多麻烦的问题,整体来讲还比较有用,但XML文档结构比较固定,耦合度还是比较高,不利于扩展,不过我对XML的大部分操作也没有那么复杂,暂且就这样吧:

类名:

CXML.ControllerXML

成员方法:
public ArrayList GetAllXMLNodes(string path,string fatherNodes,string sonNodes, string attribute,string nodes)
public void AddXMLNode(string path, string fatherNodes, string sonNodes, string[] attribute, string[] attributeValue, string[] nodes, string[] nodesValue)
public void DelXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue)
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string newAttributeValue)
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string nodes, string newAttributeValue)


 

下面是类库文件:
ControllerXML.cs


--------------------------------------------------------------------------------


/*XML文件结构示例:
 * Schedule为父节点
 * KeyData为子节点
 * year,month,day,hour,minute为属性
 * offset,value为节点
 
 

 
    100
    这是一个测试数据
 


 *
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;//引入XML命名空间
using System.Collections;//引入数组空间

namespace CXML
{
    public struct Nodes
    {
        public string attributeValue;
        public string nodeValue;
    }

    public  class ControllerXML
    {
        ///


        /// 获取父节点下所有子节点的指定属性或节点值
        ///

        /// 路径
        /// 父节点
        /// 属性名/null
        /// 节点名/null
        ///
        public ArrayList GetAllXMLNodes(string path,string fatherNodes,string sonNodes, string attribute,string nodes)
        {
            ArrayList list = new ArrayList();

            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(path);//读取XML文档
            XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name  == sonNodes)
                {
                    Nodes node = new Nodes();
                    XmlElement SNode = (XmlElement)xnf;

                    node.attributeValue = SNode.GetAttribute(attribute);

                    XmlNodeList xnf1 = SNode.ChildNodes;
                    foreach (XmlNode xn1 in xnf1)
                    {
                        if (xn1.Name == nodes)
                        {
                            node.nodeValue = xn1.InnerText;
                        }
                    }
                    list.Add(node);
                }
            }
            return list;
        }

        ///


        /// 添加子节点和添加子节点的指定的属性和节点和添加子节点到父节点
        /// 注意:属性名数组和属性值数组是对照关系,节点亦同
        ///

        /// 路径
        /// 父节点
        /// 子节点
        /// 属性名数组
        /// 属性值数组
        /// 节点名数组
        /// 节点值数组
        public void AddXMLNode(string path, string fatherNodes, string sonNodes, string[] attribute, string[] attributeValue, string[] nodes, string[] nodesValue)
        {
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(path);//读取XML文档
            XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);

            XmlElement sonData = XMLDoc.CreateElement(sonNodes);
            for (int i = 0; i             {
                sonData.SetAttribute(attribute[i], attributeValue[i]);
            }

            for (int j = 0; j             {
                XmlElement nodeData = XMLDoc.CreateElement(nodes[j]);
                nodeData.InnerText = nodesValue[j];
                //将该节点追加到上级节点
                sonData.AppendChild(nodeData); 
            }
            xn.AppendChild(sonData);
            XMLDoc.Save(path);
        }

        ///


        /// 删除子节点指定属性的所有节点和该子节点本身
        ///

        /// 路径
        /// 父节点
        /// 子节点
        /// 属性
        ///
        public void DelXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue)
        {
            ArrayList arl = new ArrayList();

            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(path);
            XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xn1 in xnl)
            {
                if (xn1.Name == sonNodes)
                {
                    XmlElement sonData = (XmlElement)xn1;
                    if (sonData.GetAttribute(attribute) == attributeValue)
                    {
                        //sonData.RemoveAllAttributes ();
                        //sonData.RemoveAll();
                        arl.Add(xn1);
                    }
                }
            }
            foreach (XmlNode xn1 in arl)
            {
                xn.RemoveChild(xn1);
            }
            XMLDoc.Save(path);
        }

       
        ///


        /// 修改指定父节点的子节点的属性值
        ///

        /// 路径
        /// 父节点
        /// 子节点
        /// 属性
        /// 属性值
        /// 新的属性值
        public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string newAttributeValue)
        {
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(path);//读取XML文档
            XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name == sonNodes)
                {
                    XmlElement sonData = (XmlElement)xnf;
                    if (sonData.GetAttribute(attribute) == attributeValue)
                    {
                        sonData.SetAttribute(attribute, newAttributeValue);
                    }
                }
            }
            XMLDoc.Save(path);
        }

        ///


        /// 修改指定属性的指定节点值(重载)
        ///

        /// 路径
        /// 父节点
        /// 子节点
        /// 属性
        /// 属性值
        /// 节点
        /// 新节点值
        public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string nodes, string newAttributeValue)
        {
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(path);//读取XML文档
            XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name == sonNodes)
                {
                    XmlElement sonData = (XmlElement)xnf;
                    if (sonData.GetAttribute(attribute) == attributeValue)
                    {
                        XmlNodeList nls = sonData.ChildNodes;
                        foreach (XmlNode xn1 in nls)
                        {
                            XmlElement node1 = (XmlElement)xn1;
                            if (node1.Name == nodes)
                            {
                                node1.InnerText = newAttributeValue;
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            XMLDoc.Save(path);
        }
    }
}

下面是一个Demo

XMLFile1.xml
--------------------------------------------------------------------------------



 
    100
    这是一个测试数据
 

 
    100
    这是一个测试数据2
 

 
    1000
    这是一个测试数据3
 

 
    100
    这是一个测试数据
 

 
    100
    这是一个测试数据2
 

 
    1000
    这是一个测试数据3
 

Program.cs
--------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using CXML;
using System.Collections;//引入数组空间

namespace Demo
{
    class Program
    {
       
        static void Main(string[] args)
        {
            /*
            //读取
            string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
            string fath ="Schedule";
            Console.WriteLine ("输入子节点:"); 
            string son =Console.ReadLine();
            Console.WriteLine ("输入属性:"); 
            string Attri= Console.ReadLine();
            Console.WriteLine("输入节点:");
            string nod = Console.ReadLine();
           
            ControllerXML cXML = new ControllerXML();
            ArrayList list = new ArrayList();
            list = cXML.GetAllXMLNodes(path,fath,son, Attri, nod);
            foreach (CXML.Nodes node in list)
            {
                Console.WriteLine("---节点---");
                Console.WriteLine("属性值为:{0}", node.attributeValue);
                Console.WriteLine("子节点值为:{0}", node.nodeValue );
            }
            Console.Read();
            
           
            //添加
            string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
            string father = "Schedule";
            string son = "KeyData";
            string[] Attri={"year","month"};
            string[] Attri_V = { "1990", "3" };
            string[] N = { "offset", "value" };
            string[] N_V = { "200", "30fgdg" };
           
            ControllerXML cXML = new ControllerXML();
            cXML.AddXMLNode(path, father, son, Attri, Attri_V,N,N_V );
            Console.WriteLine("成功");
            Console.Read();
          

            //删除
            string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
            string father = "Schedule";
            string son = "KeyData";
            string Attri = "month";
            string Attri_V = "7";
           
            ControllerXML cXML = new ControllerXML();
            cXML.DelXMLNode(path, father,son, Attri, Attri_V);
            Console.WriteLine("成功");
            Console.Read();
             

            //修改节点
            string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";

            ControllerXML cXML = new ControllerXML();
            cXML.EditXMLNode(path, "Schedule", "KeyData", "month", "7", "8");
            Console.WriteLine("成功");
            Console.Read();
             */

            //修改节点
            string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";

            ControllerXML cXML = new ControllerXML();
            cXML.EditXMLNode(path, "Schedule", "KeyData", "month", "8", "offset","修改成功");
            Console.WriteLine("成功");
            Console.Read();
        }
    }
}

下载该DLL文件的地址:http://cid-19bddddceef748b0.office.live.com/self.aspx/.Public/ControllerXML.cs.dll


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mane_yao/archive/2010/09/17/5891988.aspx

转:https://www.cnblogs.com/mane/archive/2010/09/18/1829920.html



推荐阅读
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了在Vue项目中如何结合Element UI解决连续上传多张图片及图片编辑的问题。作者强调了在编码前要明确需求和所需要的结果,并详细描述了自己的代码实现过程。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 集合的遍历方式及其局限性
    本文介绍了Java中集合的遍历方式,重点介绍了for-each语句的用法和优势。同时指出了for-each语句无法引用数组或集合的索引的局限性。通过示例代码展示了for-each语句的使用方法,并提供了改写为for语句版本的方法。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
author-avatar
SCY瑶_450
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有