Xml通用操作类
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace CommonUtil
- {
- /// <summary>
- /// Xml通用操作类
- /// </summary>
- public class XmlHelper
- {
- public XmlHelper()
- {
- }
- /// <summary>
- /// 创建XML文件
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="filename">xml文件名称(包含后缀名)</param>
- /// <param name="rootname">根节点名称</param>
- /**************************************************
- * 使用示列:
- * XmlHelper.CreateXmlFile(path, "text.xml", "root")
- ************************************************/
- public static void CreateXmlFile(string path, string filename, string rootname)
- {
- try
- {
- if (path.Equals(""))
- throw new Exception("路径不能为空!");
- if (filename.Equals(""))
- throw new Exception("文件名称不能为空!");
- if (!filename.Split('.').GetValue(filename.Split('.').Length - 1).ToString().ToLower().Equals("xml"))
- throw new Exception("文件后缀名称必须为xml!");
- if (rootname.Equals(""))
- throw new Exception("根节点名称不能为空!");
- if (!Directory.Exists(path)) //如果路径不存在,则创建路径
- Directory.CreateDirectory(path);
- StringBuilder builder = new StringBuilder();
- builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- if (!rootname.Equals(""))
- {
- builder.AppendLine("<" + rootname + ">");
- builder.AppendLine("</" + rootname + ">");
- }
- string fullpath = string.Empty;
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(builder.ToString());
- if (path.LastIndexOf('\\') == path.Length - 1)
- fullpath = path + filename;
- else
- fullpath = path + "\\" + filename;
- doc.Save(fullpath); //保存xml文件
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 读取数据
- /// </summary>
- /// <param name="path">全路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
- /// <returns>string</returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Read(path, "/Node", "")
- * XmlHelper.Read(path, "/Node/Element[@Attribute='Value']", "Attribute")
- ************************************************/
- public static string Read(string path, string node, string attribute)
- {
- string value = "";
- try
- {
- if (!File.Exists(path))
- throw new Exception("xml文件不存在!");
- if (node.Equals(""))
- throw new Exception("节点名称不能为空!");
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
- }
- catch
- {
- throw;
- }
- return value;
- }
- /// <summary>
- /// 读取数据
- /// </summary>
- /// <param name="path">全路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
- /// <returns></returns>
- public static List<string> ReadToList(string path, string node, string attribute)
- {
- List<string> nodelist = new List<string>();
- try
- {
- if (!File.Exists(path))
- throw new Exception("xml文件不存在!");
- if (node.Equals(""))
- throw new Exception("节点名称不能为空!");
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNodeList nodes = doc.SelectNodes(node);
- if (nodes != null && nodes.Count > 0)
- {
- foreach (XmlNode n in nodes)
- {
- if (attribute.Equals(""))
- nodelist.Add(n.InnerText);
- else
- nodelist.Add(n.Attributes[attribute].Value);
- }
- }
- else
- {
- nodelist = null;
- }
- }
- catch
- {
- throw;
- }
- return nodelist;
- }
- /// <summary>
- /// 插入数据
- /// </summary>
- /// <param name="path">全路径</param>
- /// <param name="node">节点</param>
- /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
- /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
- * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
- * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
- * XmlHelper.Insert(path, "/Node/Element[@Attribute='Value']", "Element", "Attribute", "Value");
- ************************************************/
- public static void Insert(string path, string node, string element, string attribute, string value)
- {
- try
- {
- if (!File.Exists(path))
- throw new Exception("xml文件不存在!");
- if (node.Equals(""))
- throw new Exception("节点名称不能为空!");
- if (element.Equals("") && attribute.Equals(""))
- throw new Exception("元素名和属性名不能同时为空!");
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- if (element.Equals(""))
- {
- if (!attribute.Equals(""))
- {
- XmlElement xe = (XmlElement)xn;
- xe.SetAttribute(attribute, value);
- }
- }
- else
- {
- XmlElement xe = doc.CreateElement(element);
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- xn.AppendChild(xe);
- }
- doc.Save(path);
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 修改数据
- /// </summary>
- /// <param name="path">全路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Insert(path, "/Node", "", "Value")
- * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
- ************************************************/
- public static void Update(string path, string node, string attribute, string value)
- {
- try
- {
- if (!File.Exists(path))
- throw new Exception("xml文件不存在!");
- if (node.Equals(""))
- throw new Exception("节点名称不能为空!");
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = (XmlElement)xn;
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- doc.Save(path);
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <param name="path">全路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Delete(path, "/Node", "")
- * XmlHelper.Delete(path, "/Node", "Attribute")
- ************************************************/
- public static void Delete(string path, string node, string attribute)
- {
- try
- {
- if (!File.Exists(path))
- throw new Exception("xml文件不存在!");
- if (node.Equals(""))
- throw new Exception("节点名称不能为空!");
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNodeList nodes = doc.SelectNodes(node);
- if (nodes != null && nodes.Count > 0)
- {
- foreach (XmlNode xn in nodes)
- {
- XmlElement xe = (XmlElement)xn;
- if (attribute.Equals(""))
- xn.ParentNode.RemoveChild(xn);
- else
- xe.RemoveAttribute(attribute);
- }
- doc.Save(path);
- }
- }
- catch
- {
- throw;
- }
- }
- }
- }
Xml通用操作类的更多相关文章
- XML文件操作类--创建XML文件
这个类是在微软XML操作类库上进行的封装,只是为了更加简单使用,包括XML类创建节点的示例. using System; using System.Collections; using System. ...
- PHP对XML文件操作类讲解
<?phpclass XML{ private $dom; function __construct () { $this->dom = new D ...
- C# XML文件操作类XmlHelper
类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...
- [No0000DE]C# XmlHelper XML类型操作 类封装
using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...
- 通用数据库操作类,前端easyui-datagrid,form
实现功能: 左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- XML转换为对象操作类详解
//XML转换为对象操作类 //一,XML与Object转换类 using System.IO; using System.Runtime.Serialization.Formatters.Binar ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- XML Helper XML操作类
写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...
随机推荐
- Css基础-派生选择器
如果要修改li strong 里面文字的颜色可以这样写样式 派生选择器: li strong { color:red; } 效果:
- 数学之路(3)-机器学习(3)-机器学习算法-SVM[5]
svm小结 1.超平面 两种颜色的点分别代表两个类别,红颜色的线表示一个可行的超平面.在进行分类的时候,我们将数据点 x 代入 f(x) 中,如果得到的结果小于 0 ,则赋予其类别 -1 ,如果 ...
- Php AES加密、解密与Java互操作的问题
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 【转】C++对象内存分配问题
原文:http://blog.csdn.net/c504665913/article/details/7797859 如果一个人自称为程序高手,却对内存一无所知,那么我可以告诉你,他一定在吹牛.用C或 ...
- CRM-BP相关FUNCTION
获取BP的地址信息:BUPA_ADDRESS_GET_DETAIL 修改BP的信息:CRM_WAP_BP_CHANGE BUTO50存放2个BP之间的关系 获取BP的角色 BAPI_BUPA_ROLE ...
- hdu 1712 ACboy needs your help 分组背包
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...
- java_jdbc_反射
package cn.itcast.Reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; imp ...
- mac mysql error You must reset your password using ALTER USER statement before executing this statement.
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个 step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER ...
- [转]让你提升命令行效率的 Bash 快捷键
生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...
- 理解hadoop的Map-Reduce数据流(data flow)
http://blog.csdn.net/yclzh0522/article/details/6859778 Map-Reduce的处理过程主要涉及以下四个部分: 客户端Client:用于提交Map- ...