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通用操作类的更多相关文章

  1. XML文件操作类--创建XML文件

    这个类是在微软XML操作类库上进行的封装,只是为了更加简单使用,包括XML类创建节点的示例. using System; using System.Collections; using System. ...

  2. PHP对XML文件操作类讲解

    <?phpclass XML{    private $dom;        function __construct ()    {        $this->dom = new D ...

  3. C# XML文件操作类XmlHelper

    类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...

  4. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  5. 通用数据库操作类,前端easyui-datagrid,form

    实现功能:     左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...

  6. 简单的XML操作类

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  7. XML转换为对象操作类详解

    //XML转换为对象操作类 //一,XML与Object转换类 using System.IO; using System.Runtime.Serialization.Formatters.Binar ...

  8. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  9. XML Helper XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...

随机推荐

  1. Volley使用指南第一回(来自developer.android)

    最近闲来想看看android网络方面的东西.google在2013年发布了一个叫做Volley的网络请求框架,我看了一下官网,居然在training里面就有教程.首先,英文的东西看着 还是挺不爽的,特 ...

  2. HDU 5476 Explore Track of Point 数学平几

    Explore Track of Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  3. (转) 如何在JavaScript与ActiveX之间传递数据1

    本文研究如何在JS等脚本语言与ActiveX控件之间通信,如何传递各种类型的参数,以及COM的IDispatch接口.使用类似的方法,可以推广到其他所有脚本型语言,如LUA,AutoCad等.本文将研 ...

  4. linux内核系统调用和标准C库函数的关系分析

    http://blog.csdn.net/skyflying2012/article/details/10044343

  5. Python built-in函数的源码实现定位

    http://blog.nsfocus.net/locate-python-built-in-function/

  6. Redirect

    Redirect To use this Class, add the following to the top of the file. use Redirect; Redirect::to($pa ...

  7. 关于JFace中的TableViewer和TreeViewer中的

    TableViewer类 构造方法摘要: 方法摘要: 在做的这几个练习中,发现,getTable(),refresh(),remove(),setSelection()方法经常使用. TreeView ...

  8. uiatuomator命令启动apk,与查找多个相同控件

    背景:在做项目时,发现使用uiatuomator中遇到了一些问题,现在把解决方法和思路分享出来 案列1:使用命令去启动要运用的apk包 在做自动化时,需要通过命令去启动APK的包,我使用的是sdk中自 ...

  9. CSS常用布局实现方法

    CSS 布局对我来说,既熟悉又陌生.我既能实现它,又没有很好的了解它.所以想总结一下,梳理一下 CSS 中常用的一列,两列,三列布局等的实现方法.本文小白,仅供参考.但也要了解下浮动,定位等. 一.一 ...

  10. scala学习笔记:高阶函数

    scala> def power(y:Double)=(x:Double)=>Math.pow(x,y) warning: there were 1 deprecation warning ...