c#操作xml帮助类分享(xml增删改查)
using system;
using system.collections;
using system.xml;
namespace jb51.com.xmldal
{
public class xmlhelper
{
#region 公共变量
xmldocument xmldoc;
xmlnode xmlnode;
xmlelement xmlelem;
#endregion
#region 创建xml文档
/// <summary>
/// 创建一个带有根节点的xml文件
/// </summary>
/// <param name="filename">xml文件名称</param>
/// <param name="rootname">根节点名称</param>
/// <param name="encode">编码方式:gb2312,utf-8等常见的</param>
/// <param name="dirpath">保存的目录路径</param>
/// <returns></returns>
public bool createxmldocument(string filename, string rootname, string encode)
{
try
{
xmldoc = new xmldocument();
xmldeclaration xmldecl;
xmldecl = xmldoc.createxmldeclaration("1.0", encode,null);
xmldoc.appendchild(xmldecl);
xmlelem = xmldoc.createelement("", rootname, "");
xmldoc.appendchild(xmlelem);
xmldoc.save(filename);
return true;
}
catch (exception e)
{
return false;
throw new exception(e.message);
}
}
#endregion
#region 常用操作方法(增删改)
/// <summary>
/// 插入一个节点和它的若干子节点
/// </summary>
/// <param name="xmlfile">xml文件路径</param>
/// <param name="newnodename">插入的节点名称</param>
/// <param name="hasattributes">此节点是否具有属性,true为有,false为无</param>
/// <param name="fathernode">此插入节点的父节点,要匹配的xpath表达式(例如:"//节点名//子节点名)</param>
/// <param name="htatt">此节点的属性,key为属性名,value为属性值</param>
/// <param name="htsubnode">子节点的属性,key为name,value为innertext</param>
/// <returns>返回真为更新成功,否则失败</returns>
public bool insertnode(string xmlfile, string newnodename, bool hasattributes, string fathernode, hashtable htatt, hashtable htsubnode)
{
try
{
xmldoc = new xmldocument();
xmldoc.load(xmlfile);
xmlnode root = xmldoc.selectsinglenode(fathernode);
xmlelem = xmldoc.createelement(newnodename);
if (htatt != null && hasattributes)//若此节点有属性,则先添加属性
{
setattributes(xmlelem, htatt);
setnodes(xmlelem.name, xmldoc, xmlelem, htsubnode);//添加完此节点属性后,再添加它的子节点和它们的innertext
}
else
{
setnodes(xmlelem.name, xmldoc, xmlelem, htsubnode);//若此节点无属性,那么直接添加它的子节点
}
root.appendchild(xmlelem);
xmldoc.save(xmlfile);
return true;
}
catch (exception e)
{
throw new exception(e.message);
}
}
/// <summary>
/// 更新节点
/// </summary>
/// <param name="xmlfile">xml文件路径</param>
/// <param name="fathernode">需要更新节点的上级节点,要匹配的xpath表达式(例如:"//节点名//子节点名)</param>
/// <param name="htatt">需要更新的属性表,key代表需要更新的属性,value代表更新后的值</param>
/// <param name="htsubnode">需要更新的子节点的属性表,key代表需要更新的子节点名字name,value代表更新后的值innertext</param>
/// <returns>返回真为更新成功,否则失败</returns>
public bool updatenode(string xmlfile, string fathernode, hashtable htatt, hashtable htsubnode)
{
try
{
xmldoc = new xmldocument();
xmldoc.load(xmlfile);
xmlnodelist root = xmldoc.selectsinglenode(fathernode).childnodes;
updatenodes(root, htatt, htsubnode);
xmldoc.save(xmlfile);
return true;
}
catch (exception e)
{
throw new exception(e.message);
}
}
/// <summary>
/// 删除指定节点下的子节点
/// </summary>
/// <param name="xmlfile">xml文件路径</param>
/// <param name="fathernode">制定节点,要匹配的xpath表达式(例如:"//节点名//子节点名)</param>
/// <returns>返回真为更新成功,否则失败</returns>
public bool deletenodes(string xmlfile, string fathernode)
{
try
{
xmldoc = new xmldocument();
xmldoc.load(xmlfile);
xmlnode = xmldoc.selectsinglenode(fathernode);
xmlnode.removeall();
xmldoc.save(xmlfile);
return true;
}
catch (xmlexception xe)
{
throw new xmlexception(xe.message);
}
}
/// <summary>
/// 删除匹配xpath表达式的第一个节点(节点中的子元素同时会被删除)
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param>
/// <returns>成功返回true,失败返回false</returns>
public bool deletexmlnodebyxpath(string xmlfilename, string xpath)
{
bool issuccess = false;
xmldoc = new xmldocument();
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnode xmlnode = xmldoc.selectsinglenode(xpath);
if (xmlnode != null)
{
//删除节点
xmldoc.parentnode.removechild(xmlnode);
}
xmldoc.save(xmlfilename); //保存到xml文档
issuccess = true;
}
catch (exception ex)
{
throw ex; //这里可以定义你自己的异常处理
}
return issuccess;
}
/// <summary>
/// 删除匹配xpath表达式的第一个节点中的匹配参数xmlattributename的属性
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlattributename">要删除的xmlattributename的属性名称</param>
/// <returns>成功返回true,失败返回false</returns>
public bool deletexmlattributebyxpath(string xmlfilename, string xpath, string xmlattributename)
{
bool issuccess = false;
bool isexistsattribute = false;
xmldoc = new xmldocument();
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnode xmlnode = xmldoc.selectsinglenode(xpath);
xmlattribute xmlattribute = null;
if (xmlnode != null)
{
//遍历xpath节点中的所有属性
foreach (xmlattribute attribute in xmlnode.attributes)
{
if (attribute.name.tolower() == xmlattributename.tolower())
{
//节点中存在此属性
xmlattribute = attribute;
isexistsattribute = true;
break;
}
}
if (isexistsattribute)
{
//删除节点中的属性
xmlnode.attributes.remove(xmlattribute);
}
}
xmldoc.save(xmlfilename); //保存到xml文档
issuccess = true;
}
catch (exception ex)
{
throw ex; //这里可以定义你自己的异常处理
}
return issuccess;
}
/*柯乐义*/
/// <summary>
/// 删除匹配xpath表达式的第一个节点中的所有属性
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param>
/// <returns>成功返回true,失败返回false</returns>
public bool deleteallxmlattributebyxpath(string xmlfilename, string xpath)
{
bool issuccess = false;
xmldoc = new xmldocument();
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnode xmlnode = xmldoc.selectsinglenode(xpath);
if (xmlnode != null)
{
//遍历xpath节点中的所有属性
xmlnode.attributes.removeall();
}
xmldoc.save(xmlfilename); //保存到xml文档
issuccess = true;
}
catch (exception ex)
{
throw ex; //这里可以定义你自己的异常处理
}
return issuccess;
}
#endregion
#region 私有方法
/// <summary>
/// 设置节点属性
/// </summary>
/// <param name="xe">节点所处的element</param>
/// <param name="htattribute">节点属性,key代表属性名称,value代表属性值</param>
private void setattributes(xmlelement xe, hashtable htattribute)
{
foreach (dictionaryentry de in htattribute)
{
xe.setattribute(de.key.tostring(), de.value.tostring());
}
}
/// <summary>
/// 增加子节点到根节点下
/// </summary>
/// <param name="rootnode">上级节点名称</param>
/// <param name="xmldoc">xml文档</param>
/// <param name="rootxe">父根节点所属的element</param>
/// <param name="subnodes">子节点属性,key为name值,value为innertext值</param>
private void setnodes(string rootnode, xmldocument xmldoc, xmlelement rootxe, hashtable subnodes)
{
if (subnodes == null)
return;
foreach (dictionaryentry de in subnodes)
{
xmlnode = xmldoc.selectsinglenode(rootnode);
xmlelement subnode = xmldoc.createelement(de.key.tostring());
subnode.innertext = de.value.tostring();
rootxe.appendchild(subnode);
}
}
/// <summary>
/// 更新节点属性和子节点innertext值。柯 乐 义
/// </summary>
/// <param name="root">根节点名字</param>
/// <param name="htatt">需要更改的属性名称和值</param>
/// <param name="htsubnode">需要更改innertext的子节点名字和值</param>
private void updatenodes(xmlnodelist root, hashtable htatt, hashtable htsubnode)
{
foreach (xmlnode xn in root)
{
xmlelem = (xmlelement)xn;
if (xmlelem.hasattributes)//如果节点如属性,则先更改它的属性
{
foreach (dictionaryentry de in htatt)//遍历属性哈希表
{
if (xmlelem.hasattribute(de.key.tostring()))//如果节点有需要更改的属性
{
xmlelem.setattribute(de.key.tostring(), de.value.tostring());//则把哈希表中相应的值value赋给此属性key
}
}
}
if (xmlelem.haschildnodes)//如果有子节点,则修改其子节点的innertext
{
xmlnodelist xnl = xmlelem.childnodes;
foreach (xmlnode xn1 in xnl)
{
xmlelement xe = (xmlelement)xn1;
foreach (dictionaryentry de in htsubnode)
{
if (xe.name == de.key.tostring())//htsubnode中的key存储了需要更改的节点名称,
{
xe.innertext = de.value.tostring();//htsubnode中的value存储了key节点更新后的数据
}
}
}
}
}
}
#endregion
#region xml文档节点查询和读取
/// <summary>
/// 选择匹配xpath表达式的第一个节点xmlnode.
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名")</param>
/// <returns>返回xmlnode</returns>
public xmlnode getxmlnodebyxpath(string xmlfilename, string xpath)
{
xmldoc = new xmldocument();
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnode xmlnode = xmldoc.selectsinglenode(xpath);
return xmlnode;
}
catch (exception ex)
{
return null;
//throw ex; //这里可以定义你自己的异常处理
}
}
/// <summary>
/// 选择匹配xpath表达式的节点列表xmlnodelist.
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名")</param>
/// <returns>返回xmlnodelist</returns>
public xmlnodelist getxmlnodelistbyxpath(string xmlfilename, string xpath)
{
xmldoc = new xmldocument();
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnodelist xmlnodelist = xmldoc.selectnodes(xpath);
return xmlnodelist;
}
catch (exception ex)
{
return null;
//throw ex; //这里可以定义你自己的异常处理
}
}
/// <summary>
/// 选择匹配xpath表达式的第一个节点的匹配xmlattributename的属性xmlattribute. 柯乐义
/// </summary>
/// <param name="xmlfilename">xml文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的xpath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlattributename">要匹配xmlattributename的属性名称</param>
/// <returns>返回xmlattributename</returns>
public xmlattribute getxmlattribute(string xmlfilename, string xpath, string xmlattributename)
{
string content = string.empty;
xmldoc = new xmldocument();
xmlattribute xmlattribute = null;
try
{
xmldoc.load(xmlfilename); //加载xml文档
xmlnode xmlnode = xmldoc.selectsinglenode(xpath);
if (xmlnode != null)
{
if (xmlnode.attributes.count > 0)
{
xmlattribute = xmlnode.attributes[xmlattributename];
}
}
}
catch (exception ex)
{
throw ex; //这里可以定义你自己的异常处理
}
return xmlattribute;
}
#endregion
}
}
这个类怎么使用呢?下面给出一个创建xml文档的例子:
xmlhelper m_menu_keleyi_com = new xmlhelper();
m_menu_keleyi_com.createxmldocument(@"d:\kel"+"eyimenu.xml", "ke"+"leyimenu", "utf-8");
这短代码在d盘创建了一个名为keleyimenu.xml的文档,文档有根节点keleyimenu,文档的内容为:
<?xml version="1.0" encoding="utf-8"?>
<keleyimenu />
上一篇: c#定时运行程序分享(定时程序)