开面见山吧!我也就不废话了!不喜勿喷哈!!!!
新建一个Class(我这里是“XMLHelp”)
引入命名空间
using System.Xml;
class XMLHelp
{
public static XmlDocument xdoc;//xml对象
public static XmlNode xnode;//xml中的节点
public static XmlElement xelement;//xml中的元素
public static string path;
//默认的构造函数
public XMLHelp() { }
/// <summary>
/// 加载xml文件
/// </summary>
/// <param name="path">路径</param>
public static void LoadXml(string path)
{
xdoc = new XmlDocument();
xdoc.Load(path);
}
/// <summary>
/// 保存xml文件
/// </summary>
public static void SavedXml(string path)
{
xdoc.Save(path);
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>返回值</returns>
public static string Read(string node,string attribute)
{
LoadXml(path);
string value = "";
try
{
xnode = xdoc.SelectSingleNode(node);
value = (attribute.Equals("") ? xnode.InnerText : xnode.Attributes[attribute].Value);//?:三元运算符
}catch
{
throw;
}
return value;
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">插入值</param>
public static void Insert(string node,string element,string attribute,string value)
{
try
{
LoadXml(path);
xnode = xdoc.SelectSingleNode(node);
//如果元素为空,属性不为空,则赋予属性值
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
xelement = (XmlElement)xnode;
xelement.SetAttribute(attribute, value);
}
}
else
{
xelement = xdoc.CreateElement(element);
//属性为空,创建一个元素并赋与值,属性不为空,则赋予属性值
if (attribute.Equals(""))
{
xelement.InnerText = value;
}
else
{
xelement.SetAttribute(attribute, value);
}
xnode.AppendChild(xelement);
}
SavedXml(path);
}catch
{
throw;
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">新值</param>
public static void Update(string node,string attribute,string value)
{
try
{
LoadXml(path);
xnode = xdoc.SelectSingleNode(node);
xelement = (XmlElement)xnode;
if (attribute.Equals(""))
{
xelement.InnerText = value;
}
else
{
xelement.SetAttribute(attribute, value);
}
SavedXml(path);
}catch
{
throw;
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
public static void Delete(string node, string attribute)
{
try
{
LoadXml(path);
xnode = xdoc.SelectSingleNode(node);
xelement = (XmlElement)xnode;
if (attribute.Equals(""))
xnode.ParentNode.RemoveChild(xnode);
else
xelement.RemoveAttribute(attribute);
SavedXml(path);
}catch
{
throw;
}
}
}
******************************************************************************************************
以下是这个XMLHelp的使用方法(代码已经实例测试可行)
static void Main(string[] args)
{
// 给出xml的路径
string path = "../../XMLFile/XMLFile1.xml";
XMLHelp.path = path;
/**
* 插入
*/
// XMLHelp.Insert("/Root", "student", "", "");//插入名为“student”的元素没有属性和值
// XMLHelp.Insert("/Root", "student", "Name", "coolfei");//插入元素/属性为“Name”属性值为“coolfei”
// XMLHelp.Insert("/Root/student", "name", "", "coolfei");//插入元素为“name”值为“coolfei”
/**
* 修改
*/
// XMLHelp.Update("/Root/student", "Name", "cool");//修改Name属性的值为“cool”
// XMLHelp.Update("/Root/student/name", "", "cool");//修改元素的值为“cool”
/**
* 查询
*/
// string str1 = XMLHelp.Read("/Root/student", "");//读取一节点下所有的值
// string str2 = XMLHelp.Read("/Root/student", "Name");//读取一节中名为“Name”的属性值
/**
* 删除
*/
// XMLHelp.Delete("/Root/student", "Name");//删除名为“Name”属性
// XMLHelp.Delete("/Root/student/name", "");//删除名为“name”的元素
}