仿Jquery链式操作的xml操作类

经常需要对xml文件进行操作,参考了Jquery的链式操作后实现了xmlHelper类。

ExpandedBlockStart.gif代码
using System;
using System.Data;
using System.Configuration;
using System.Xml;
namespace ConfigUpdate
{
    
/// <summary>
    
/// 调用非静态的操作方法的
    
/// </summary>
    public class XmlHelper
    {
        
private XmlDocument _doc;
        
private string _xmlPath;
        
public XmlHelper(string xmlPath)
        {
            _xmlPath 
= xmlPath;
            _doc 
= new XmlDocument();
            _doc.Load(xmlPath);
        }

        
/// <summary>
        
/// 读取数据
        
/// </summary>
        
/// <param name="xmlPath">路径</param>
        
/// <param name="node">节点</param>
        
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
        
/// <returns>string</returns>
        public static string Select(string xmlPath, string node, string attribute)
        {
            XmlDocument doc 
= new XmlDocument();
            doc.Load(xmlPath);
            XmlNode xn 
= GetNode(doc, node);
            
string value = (String.IsNullOrEmpty(attribute) ? xn.InnerText : xn.Attributes[attribute].Value);
            doc.Clone();
            
return value;
        }

        
/// <summary>
        
/// 调用此方法后不再需要调用Execute方法。
        
/// </summary>
        
/// <param name="node"></param>
        
/// <param name="attribute"></param>
        
/// <returns></returns>
        public string Select(string node, string attribute)
        {
            
string value = String.Empty; ;
            XmlNode xn 
= GetNode(_doc, node);
            value 
= (String.IsNullOrEmpty(attribute) ? xn.InnerText : xn.Attributes[attribute].Value);
            
return value;
        }

        
/// <summary>
        
/// 插入数据
        
/// </summary>
        
/// <param name="xmlPath">路径</param>
        
/// <param name="node">节点</param>
        
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
        
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public XmlHelper Insert(string node, string element, string attribute, string value)
        {
            Add(_doc, _xmlPath, node, element, attribute, value);
            
return this;
        }

        
public static void Insert(string xmlPath, string node, string element, string attribute, string value)
        {
            XmlDocument doc 
= new XmlDocument();
            doc.Load(xmlPath);
            Add(doc, xmlPath, node, element, attribute, value);
        }

        
private static void Add(XmlDocument doc, string xmlPath, string node, string element, string attribute, string value)
        {
            XmlNode xn 
= GetNode(doc, node);
            
if (String.IsNullOrEmpty(element))
            {
                
if (!String.IsNullOrEmpty(attribute))
                {
                    XmlElement xe 
= (XmlElement)xn;
                    xe.SetAttribute(attribute, value);
                }
            }
            
else
            {
                XmlElement xe 
= doc.CreateElement(element);
                
if (String.IsNullOrEmpty(attribute))
                    xe.InnerText 
= value;
                
else
                    xe.SetAttribute(attribute, value);
                xn.AppendChild(xe);
            }
            doc.Save(xmlPath);
        }

        
/// <summary>
        
/// 修改数据
        
/// </summary>
        
/// <param name="xmlPath">路径</param>
        
/// <param name="node">节点</param>
        
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public static void Update(string xmlPath, string node, string attribute, string value)
        {
            XmlDocument doc 
= new XmlDocument();
            doc.Load(xmlPath);
            Edit(doc, xmlPath, node, attribute, value);
        }

        
/// <summary>
        
/// 插入一个节点或者属性
        
/// </summary>
        
/// <param name="node"></param>
        
/// <param name="attribute"></param>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public XmlHelper Update(string node, string attribute, string value)
        {
            Edit(_doc, _xmlPath, node, attribute, value);
            
return this;
        }

        
private static void Edit(XmlDocument doc, string xmlPath, string node, string attribute, string value)
        {
            XmlNode xn 
= GetNode(doc, node);
            XmlElement xe 
= (XmlElement)xn;
            
if (String.IsNullOrEmpty(attribute))
            {
                xe.InnerText 
= value;
            }
            
else
            {
                xe.SetAttribute(attribute, value);
            }
            doc.Save(xmlPath);
        }
       
        
/// <summary>
        
/// 删除数据
        
/// </summary>
        
/// <param name="xmlPath">路径</param>
        
/// <param name="node">节点如果是非根节点可使用多层节点表达式:nodelayer1>nodelayer2>nodelayer3</param>
        
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public static void Remove(string xmlPath, string node, string attribute)
        {
            XmlDocument doc 
= new XmlDocument();
            doc.Load(xmlPath);
            Delete(doc, xmlPath, node, attribute);
        }

        
public XmlHelper Delete(string node, string attribute)
        {
            Delete(_doc, _xmlPath, node, attribute);
            
return this;
        }

        
private static void Delete(XmlDocument doc, string xmlPath, string node, string attribute)
        {
            XmlNode xn 
= GetNode(doc, node);
            XmlElement xe 
= (XmlElement)xn;
            
if (String.IsNullOrEmpty(attribute))
            {
                xn.ParentNode.RemoveChild(xn);
            }
            
else
            {
                xe.RemoveAttribute(attribute);
            }
            doc.Save(xmlPath);
        }
        
/// <summary>
        
/// 返回节点
        
/// </summary>
        
/// <param name="doc"></param>
        
/// <param name="node">节点表达式</param>
        
/// <returns></returns>
        private static XmlNode GetNode(XmlDocument doc, string node)
        {
            
if (String.IsNullOrEmpty(node))
            {
                
throw new ArgumentNullException("节点不能为空");
            }
            
string[] nodelayers = node.Split('>');
            XmlNode xn 
= doc.SelectSingleNode(nodelayers[0]);
            
for (int i = 1; i < nodelayers.Length; i++)
            {
                
if (String.IsNullOrEmpty(nodelayers[i]))
                {
                    
throw new ArgumentException(String.Format("第{0}级节点为空",i+1));
                }
                
else
                {
                    xn 
= xn.SelectSingleNode(nodelayers[i]);
                }
            }
            
return xn;
        }
    }
}

 

 使用示例:

ExpandedBlockStart.gif代码
string _configPath = "config.xml";
           XmlHelper.Insert(_configPath, 
"configuration","mynode",null,"kk");
 
           
string msg= new XmlHelper(_configPath)
               .Insert(
"configuration""mynode"null"kk")
               .Update(
"configuration>mynode"null"zhao")
               .Select(
"configuration>mynode"null);
           MessageBox.Show(msg);
   

 

 xml内容

 <?xml version="1.0" encoding="utf-8"?>

<configuration>
</configuration>

 

转载于:https://www.cnblogs.com/luckuny/archive/2011/02/22/1961255.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/475745.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

4月17日鸿蒙开发者大会,4月17日这天,将载入华为史册

文/笨鸟原创不易&#xff0c;禁止抄袭、洗稿&#xff0c;违者必究&#xff01;万众瞩目的华为鸿蒙系统对于国人来说&#xff0c;一部智能手机只有实现了芯片和系统技术的自主化&#xff0c;才能被称之为真正的国产手机。而就目前的国内手机市场而言&#xff0c;除华为之外的所有…

LeetCode 875. 爱吃香蕉的珂珂(二分查找)

1. 题目 珂珂喜欢吃香蕉。这里有 N 堆香蕉&#xff0c;第 i 堆中有 piles[i] 根香蕉。警卫已经离开了&#xff0c;将在 H 小时后回来。 珂珂可以决定她吃香蕉的速度 K &#xff08;单位&#xff1a;根/小时&#xff09;。每个小时&#xff0c;她将会选择一堆香蕉&#xff0c;…

MsSql正反表达式

例子&#xff1a; UPDATE [Photo_Table] SET istopistop^1,IsTopDateTimegetdate() WHERE charindex(, rtrim(PHOTOID) , , , PHOTOIDLIST ,)>0转载于:https://www.cnblogs.com/yibinboy/archive/2011/02/22/1961675.html

LeetCode 1455. 检查单词是否为句中其他单词的前缀

1. 题目 给你一个字符串 sentence 作为句子并指定检索词为 searchWord &#xff0c;其中句子由若干用 单个空格 分隔的单词组成。 请你检查检索词 searchWord 是否为句子 sentence 中任意单词的前缀。 如果 searchWord 是某一个单词的前缀&#xff0c;则返回句子 sentence 中…

html怎么修改锚点的属性,在HTML中设置自定义锚点

我已经在帖子here和here中看到了这个话题,但它们并没有真正帮助我.情况非常相似&#xff1a;页面顶部的滚动页面和粘性菜单栏(固定div),锚点分散在长滚动文本中.像这样的HTML&#xff1a;Heading Foothis is some text, and a lot of it...jump to Heading Foo...Heading Blaan…

PHP输出Excel实例代码

这里使用PHPExcel的开源类 一个完整的实例&#xff1a; <?php require_once("../includes/function.php"); //提供了SQL注入检测函数inject_check require_once("../class/DB.php"); //DB操作类&#xff0c;自己扩展一下 $db new DB(); if($_GET[sho…

LeetCode 1456. 定长子串中元音的最大数目(滑动窗口)

1. 题目 给你字符串 s 和整数 k 。 请返回字符串 s 中长度为 k 的单个子字符串中可能包含的最大元音字母数。 英文中的 元音字母 为&#xff08;a, e, i, o, u&#xff09;。 示例 1&#xff1a; 输入&#xff1a;s "abciiidef", k 3 输出&#xff1a;3 解释&a…

Xml文档添加节点和属性

在实际的应用开发中需要我们对xml进行添加节点和属性&#xff0c;动态的去完成&#xff0c;在这之前&#xff0c;先看看XmlNode和XmlElement之间的关系 1、XmlElement继承XmlLinkedNode又继承XmlNode&#xff0c;所以XmlElement是XmlNode的子集&#xff0c;那么从继承的关系来说…

html仿手机界面,javascript新手实例3-仿手机聊天界面(if else运用)

今天给大家一个if else的Javascript小示例&#xff0c;其中我在js文件里写了很多注释&#xff0c;有兴趣的同学自己看注释&#xff0c;另外对于聊天界面的显示方式&#xff0c;我写了两种&#xff0c;大家也可以分别试试&#xff1a;老规矩&#xff0c;先上图&#xff1a;html代…

Android开发环境搭建

刚刚接触Android&#xff0c;发现学的java都忘得差不多了~~分享一下android开发环境的搭建。 1.准备&#xff1a; 1)java sdk1.6 http://www.oracle.com/technetwork/java/javase/downloads/index.html 2)android sdk-windows http://developer.android.com/sdk/index.html 3)…

LeetCode 1457. 二叉树中的伪回文路径(位运算+递归)

1. 题目 给你一棵二叉树&#xff0c;每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「伪回文」的&#xff0c;当它满足&#xff1a;路径经过的所有节点值的排列中&#xff0c;存在一个回文序列。 请你返回从根到叶子节点的所有路径中 伪回文 路径的数目。 示例 1&…

傅立叶变换计算机网络,中南大学 计算机网络与信号处理考试卷

计算机网络复习习题1一&#xff0e;选择题1、采用全双工工作&#xff0c;数据的传输方向为&#xff1a;()A&#xff0e;双向B。单向C。双向&#xff0c;但不能同时传输D。都不对2、BSC规程采用的成帧方式为()A&#xff0e;字节记数法B。使用字符填充的首尾定界符法C&#xff0c…

LeetCode 1458. 两个子序列的最大点积(动态规划,类似编辑距离)

1. 题目 给你两个数组 nums1 和 nums2 。 请你返回 nums1 和 nums2 中两个长度相同的 非空 子序列的最大点积。 数组的非空子序列是通过删除原数组中某些元素&#xff08;可能一个也不删除&#xff09;后剩余数字组成的序列&#xff0c;但不能改变数字间相对顺序。 比方说&a…

编译html成qch,在应用程序编译过程中运行qcollectiongenerator

我一直在研究一个名为RoboJournal的程序很长一段时间。下一版本包含完整的文档;每当用户按F1或单击RoboJournal程序中的帮助项目时&#xff0c;帮助文件将显示在Qt助手中(比简单地打开浏览器窗口以获得一些联机文档更加方便)。在应用程序编译过程中运行qcollectiongenerator在其…

LeetCode 826. 安排工作以达到最大收益(map)

1. 题目 有一些工作&#xff1a;difficulty[i] 表示第i个工作的难度&#xff0c;profit[i]表示第i个工作的收益。 现在我们有一些工人。worker[i]是第i个工人的能力&#xff0c;即该工人只能完成难度小于等于worker[i]的工作。 每一个工人都最多只能安排一个工作&#xff0c…

Unparsed aapt error(s)! Check the console for output

现象&#xff1a;eclipse项目上有红叉&#xff0c;却找不到哪个文件有错。 eclipse中有“problems”的窗口&#xff0c;一般和“console”并列&#xff0c;上面会找到红叉叉的问题所在&#xff08;Unparsed aapt error(s)! Check the console for output"&#xff09; 原因…

LeetCode 837. 新21点(动态规划)

文章目录1. 题目2. 解题2.1 暴力超时2.2 优化1. 题目 爱丽丝参与一个大致基于纸牌游戏 “21点” 规则的游戏&#xff0c;描述如下&#xff1a; 爱丽丝以 0 分开始&#xff0c;并在她的得分少于 K 分时抽取数字。 抽取时&#xff0c;她从 [1, W] 的范围中随机获得一个整数作为…

sql 查看Oralce 数据库连接状态

select sid,serial#,username,program,machine,status from v$session order by username转载于:https://www.cnblogs.com/xinlang/archive/2011/02/28/1966819.html

LeetCode 793. 阶乘函数后K个零(二分查找)

1. 题目 f(x) 是 x! 末尾是0的数量。&#xff08;回想一下 x! 1 * 2 * 3 * ... * x&#xff0c;且0! 1&#xff09; 例如&#xff0c; f(3) 0 &#xff0c;因为3! 6的末尾没有0&#xff1b;而 f(11) 2 &#xff0c;因为11! 39916800末端有2个0。给定 K&#xff0c;找出多…

海量数据库解决方案2011030101

【摘抄】索引的真正意义&#xff1a;索引是优化器在制定执行计划时&#xff0c;为了寻找最优化的路径而使用的战略要素。类型&#xff1a;B-Tree索引位图索引(Bitmap)B-Tree聚簇索引哈希聚簇索引反向键索引位图连接索引基于自定义函数的索引B-Tree索引最主要的特征就是不论表中…