经常需要对xml文件进行操作,参考了Jquery的链式操作后实现了xmlHelper类。
代码
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;
}
}
}
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;
}
}
}
使用示例:
代码
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);
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>
</configuration>