using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Xml;
namespace StandardLibrary
{
/*************************************************************************
注:XML元素节点名称命名规则
1.名称的开头必须是字母、汉字或下划线“_”;
2.标记名称中不能有空格;
3.名称的字符串只能包含字母,数字,“_”,“-”,“.”;
**************************************************************************/
///
/// XML操作类
///
class XmlHandle
{
XmlDocument xmlDoc = new XmlDocument();
private string fileName; //路径+XML文件名
public XmlHandle(string fileName)
{
this.fileName = fileName;
}
///
/// 打开XML文件
///
public string Open()
{
try
{
xmlDoc.Load(fileName);
return null;
}
catch (Exception ex) { return ex.Message; }
}
///
/// 保存XML文件
///
///
public string Save()
{
try
{
xmlDoc.Save(fileName);
return null;
}
catch (Exception ex) { return ex.Message; }
}
#region 写操作
///
/// 添加根节点
///
/// 根节点名
///
public XmlNode AddRootNode(string rootName)
{
XmlElement root = (XmlElement)GetRootNode();
if (xmlDoc.FirstChild == null)
{
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");//创建类型声明节点
xmlDoc.AppendChild(node);
}
if (root == null)
{
root = xmlDoc.CreateElement(rootName); //创建根节点
xmlDoc.AppendChild(root);
}
return (XmlNode)root;
}
///
/// 添加节点
///
/// 父节点
/// 节点名
/// 节点值
///
public XmlNode AddChildNode(XmlNode parentNode, string name, string value = null)
{
XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentNode.AppendChild(node);
return node;
}
///
/// 删除节点
///
///
public void RemoveNode(XmlNode node)
{
node.ParentNode.RemoveChild(node);
}
///
/// 删除父节点下所有子节点
///
public void RemoveAllNode(XmlNode parentNode)
{
parentNode.RemoveAll();
}
///
/// 设置节点的属性
///
/// 节点
/// 属性名
/// 属性值
public void SetNodeAttribute(XmlNode node, string name, string attribute)
{
XmlElement n = (XmlElement)node;
n.SetAttribute(name, attribute);
}
///
/// 删除节点的属性
///
/// 节点
/// 属性名
public void RemoveNoedAttribute(XmlNode node, string name)
{
XmlElement n = (XmlElement)node;
n.RemoveAttribute(name);
}
///
/// 设置节点的值
///
/// 节点
/// 设置值
public void SetNodeValue(XmlNode node, string value)
{
node.InnerText = value;
}
#endregion
#region 读操作
//public XmlDeclaration GetDeclarationNode()
//{
// return xmlDoc.
//}
///
/// 获取根节点
///
///
public XmlNode GetRootNode()
{
return xmlDoc.DocumentElement;
}
///
/// 获取父节点下指定的子节点
///
/// 父节点
/// 节点名
///
public XmlNode GetCertainChildNode(XmlNode parentNode, string nodeName)
{
return parentNode.SelectSingleNode(nodeName);
}
///
/// 获取父节点的第一个子节点
///
/// 父节点
///
public XmlNode GetFirstChildNode(XmlNode parentNode)
{
return parentNode.FirstChild;
}
///
/// 获取父节点的所有子节点
///
/// 父节点
///
public XmlNodeList GetAllChildNode(XmlNode parentNode)
{
return parentNode.ChildNodes;
}
///
/// 获取节点的属性
///
/// 节点
/// 属性名
///
public string GetNodeAttribute(XmlNode node, string name)
{
XmlElement n = (XmlElement)node;
return n.GetAttribute(name);
}
///
/// 获取节点的值
///
/// 节点
///
public string GetNodeValue(XmlNode node)
{
return node.InnerText;
}
///
/// 获取节点的名称
///
/// 节点
///
public string GetNodeName(XmlNode node)
{
return node.Name;
}
#endregion
}
}