c#对XML文件的操作基础类
应用中,需要对XML文件进行操作,于是就找了个针对xml的操作基类,在实际运用中发现它还有一些需要完善的地方,所以我就在其的基础上完善了一下,反正对我来说已经实现了项目中针对xml的操作.代码如下:
/**//**********************************************************************************
*
* 功能说明:XML处理基类
* 作者: 刘功勋;
* 版本:V0.1(C#2.0);时间:2006-12-13
*
* ---------修改:hya 时间:2007-8-20
* *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
/**//// <summary>
/// XML 操作基类
/// </summary>
public class XmlHelper : IDisposable
{
//以下为单一功能的静态类
读取XML到DataSet#region 读取XML到DataSet
/**//// <summary>
/// 功能:读取XML到DataSet中
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <returns>DataSet</returns>
public static DataSet GetXml(string XmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@XmlPath);
return ds;
}
#endregion
读取xml文档并返回一个节点#region 读取xml文档并返回一个节点
/**//// <summary>
/// 读取xml文档并返回一个节点:适用于一级节点
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="NodeName">节点</param>
/// <returns></returns>
public static string ReadXmlReturnNode(string XmlPath, string Node)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn = docXml.GetElementsByTagName(Node);
return xn.Item(0).InnerText.ToString();
}
#endregion
查找数据,返回一个DataSet#region 查找数据,返回一个DataSet
/**//// <summary>
/// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
/// <returns></returns>
public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds;
}
#endregion
更新Xml节点内容#region 更新Xml节点内容
/**//// <summary>
/// 更新Xml节点内容
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="Node">要更换内容的节点:节点路径 根节点/父节点/当前节点</param>
/// <param name="Content">新的内容</param>
public static void XmlNodeReplace(string xmlPath, string Node, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
objXmlDoc.SelectSingleNode(Node).InnerText = Content;
objXmlDoc.Save(xmlPath);
}
#endregion
更改节点的属性值#region 更改节点的属性值
/**//// <summary>
/// 更改节点的属性值
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要更改的节点属性的名称</param>
/// <param name="NodeAttributeText">更改的属性值</param>
public static void XmlAttributeEide(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
#endregion
删除XML节点和此节点下的子节点#region 删除XML节点和此节点下的子节点
/**//// <summary>
/// 删除XML节点和此节点下的子节点
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="Node">节点路径</param>
public static void XmlNodeDelete(string xmlPath, string Node)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
objXmlDoc.Save(xmlPath);
}
#endregion
删除一个节点的属性#region 删除一个节点的属性
/**//// <summary>
/// 删除一个节点的属性
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径(xpath)</param>
/// <param name="NodeAttribute">属性名称</param>
public static void xmlnNodeAttributeDel(string xmlPath, string NodePath, string NodeAttribute)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.RemoveAttribute(NodeAttribute);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一个节点和此节点的子节点#region 插入一个节点和此节点的子节点
/**//// <summary>
/// 插入一个节点和此节点的字节点
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="MailNode">当前节点路径</param>
/// <param name="ChildNode">新插入节点</param>
/// <param name="Element">插入节点的子节点</param>
/// <param name="Content">子节点的内容</param>
public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
向一个节点添加属性#region 向一个节点添加属性
/**//// <summary>
/// 向一个节点添加属性
/// </summary>
/// <param name="xmlPath">xml文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要添加的节点属性的名称</param>
/// <param name="NodeAttributeText">要添加属性的值</param>
public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一节点,带一属性#region 插入一节点,带一属性
/**//// <summary>
/// 插入一节点,带一属性
/// </summary>
/// <param name="xmlPath">Xml文档路径</param>
/// <param name="MainNode">当前节点路径</param>
/// <param name="Element">新节点</param>
/// <param name="Attrib">属性名称</param>
/// <param name="AttribContent">属性值</param>
/// <param name="Content">新节点值</param>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一节点不带属性#region 插入一节点不带属性
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
在根节点下添加父节点#region 在根节点下添加父节点
/**//// <summary>
/// 在根节点下添加父节点
/// </summary>
public static void AddParentNode(string xmlPath,string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
// 创建一个新的menber节点并将它添加到根节点下
XmlElement Node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(Node);
xdoc.Save(xmlPath);
}
#endregion
//必须创建对象才能使用的类
private bool _alreadyDispose = false;
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode xmlNode;
private XmlElement xmlElem;
构造与释构#region 构造与释构
public XmlHelper()
{
}
~XmlHelper()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
//
}
_alreadyDispose = true;
}
#endregion
IDisposable 成员#region IDisposable 成员
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
创建xml文档#region 创建xml文档
创建一个只有声明和根节点的XML文档#region 创建一个只有声明和根节点的XML文档
/**//// <summary>
/// 创建一个只有声明和根节点的XML文档
/// </summary>
/// <param name="root"></param>
public void CreateXmlRoot(string root)
{
//加入XML的声明段落
xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlDoc.AppendChild(xmlNode);
//加入一个根元素
xmlElem = xmlDoc.CreateElement("", root, "");
xmlDoc.AppendChild(xmlElem);
}
#endregion
在当前节点下插入一个空节点节点#region 在当前节点下插入一个空节点节点
/**//// <summary>
/// 在当前节点下插入一个空节点节点
/// </summary>
/// <param name="mainNode">当前节点路径</param>
/// <param name="node">节点名称</param>
public void CreatXmlNode(string mainNode, string node)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
MainNode.AppendChild(objElem);
}
#endregion
在当前节点插入一个仅带值的节点#region 在当前节点插入一个仅带值的节点
/**//// <summary>
/// 在当前节点插入一个仅带值的节点
/// </summary>
/// <param name="mainNode">当前节点</param>
/// <param name="node">新节点</param>
/// <param name="content">新节点值</param>
public void CreatXmlNode(string mainNode, string node, string content)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
objElem.InnerText = content;
MainNode.AppendChild(objElem);
}
#endregion
在当前节点的插入一个仅带属性值的节点#region 在当前节点的插入一个仅带属性值的节点
/**//// <summary>
/// 在当前节点的插入一个仅带属性值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">新节点</param>
/// <param name="Attrib">新节点属性名称</param>
/// <param name="AttribValue">新节点属性值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
mainNode.AppendChild(objElem);
}
#endregion
创建一个带属性值的节点值的节点#region 创建一个带属性值的节点值的节点
/**//// <summary>
/// 创建一个带属性值的节点值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称</param>
/// <param name="AttribValue">属性值</param>
/// <param name="Content">节点传情</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
在当前节点的插入一个仅带2个属性值的节点#region 在当前节点的插入一个仅带2个属性值的节点
/**//// <summary>
/// 在当前节点的插入一个仅带2个属性值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称一</param>
/// <param name="AttribValue">属性值一</param>
/// <param name="Attrib2">属性名称二</param>
/// <param name="AttribValue2">属性值二</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
mainNode.AppendChild(objElem);
}
#endregion
在当前节点插入带两个属性的节点#region 在当前节点插入带两个属性的节点
/**//// <summary>
/// 在当前节点插入带两个属性的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称一</param>
/// <param name="AttribValue">属性值一</param>
/// <param name="Attrib2">属性名称二</param>
/// <param name="AttribValue2">属性值二</param>
/// <param name="Content">节点值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2, string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
保存Xml#region 保存Xml
/**//// <summary>
/// 保存Xml
/// </summary>
/// <param name="path">保存的当前路径</param>
public void XmlSave(string path)
{
xmlDoc.Save(path);
}
#endregion
#endregion
读取指定节点的指定属性值#region 读取指定节点的指定属性值
/**//// <summary>
/// 功能:
/// 读取指定节点的指定属性值
/// </summary>
/// <param name="strNode">节点路径</param>
/// <param name="strAttribute">此节点的属性</param>
/// <returns></returns>
public string GetXmlNodeAttribute(string xmlPath, string strNode, string strAttribute)
{
string strReturn = "";
xmlDoc.Load(xmlPath);
try
{
//根据指定路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
//获取节点的属性,并循环取出需要的属性值
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for (int i = 0; i < xmlAttr.Count; i++)
{
if (xmlAttr.Item(i).Name == strAttribute)
strReturn = xmlAttr.Item(i).Value;
}
}
catch (XmlException xmle)
{
throw xmle;
}
return strReturn;
}
#endregion
读取指定节点的值#region 读取指定节点的值
/**//// <summary>
/// 功能:
/// 读取指定节点的值
/// </summary>
/// <param name="strNode">节点名称(xpath)</param>
/// <returns></returns>
public string GetXmlNodeValue(string xmlPath,string strNode)
{
string strReturn = String.Empty;
xmlDoc.Load(xmlPath);
try
{
//根据路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
strReturn = xmlNode.InnerText;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return strReturn;
}
#endregion
根据节点属性读取子节点值(较省资源模式)#region 根据节点属性读取子节点值(较省资源模式)
/**//// <summary>
/// 根据节点属性读取子节点值(较省资源模式)
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="FatherElement">父节点值</param>
/// <param name="AttributeName">属性名称</param>
/// <param name="AttributeValue">属性值</param>
/// <param name="ArrayLength">返回的数组长度</param>
/// <returns></returns>
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn;
xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
XmlNodeList xx = xn.Item(0).ChildNodes;
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{
al.Add(xx.Item(i).InnerText);
}
return al;
}
#endregion
}
*
* 功能说明:XML处理基类
* 作者: 刘功勋;
* 版本:V0.1(C#2.0);时间:2006-12-13
*
* ---------修改:hya 时间:2007-8-20
* *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
/**//// <summary>
/// XML 操作基类
/// </summary>
public class XmlHelper : IDisposable
{
//以下为单一功能的静态类
读取XML到DataSet#region 读取XML到DataSet
/**//// <summary>
/// 功能:读取XML到DataSet中
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <returns>DataSet</returns>
public static DataSet GetXml(string XmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@XmlPath);
return ds;
}
#endregion
读取xml文档并返回一个节点#region 读取xml文档并返回一个节点
/**//// <summary>
/// 读取xml文档并返回一个节点:适用于一级节点
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="NodeName">节点</param>
/// <returns></returns>
public static string ReadXmlReturnNode(string XmlPath, string Node)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn = docXml.GetElementsByTagName(Node);
return xn.Item(0).InnerText.ToString();
}
#endregion
查找数据,返回一个DataSet#region 查找数据,返回一个DataSet
/**//// <summary>
/// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
/// <returns></returns>
public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds;
}
#endregion
更新Xml节点内容#region 更新Xml节点内容
/**//// <summary>
/// 更新Xml节点内容
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="Node">要更换内容的节点:节点路径 根节点/父节点/当前节点</param>
/// <param name="Content">新的内容</param>
public static void XmlNodeReplace(string xmlPath, string Node, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
objXmlDoc.SelectSingleNode(Node).InnerText = Content;
objXmlDoc.Save(xmlPath);
}
#endregion
更改节点的属性值#region 更改节点的属性值
/**//// <summary>
/// 更改节点的属性值
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要更改的节点属性的名称</param>
/// <param name="NodeAttributeText">更改的属性值</param>
public static void XmlAttributeEide(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
#endregion
删除XML节点和此节点下的子节点#region 删除XML节点和此节点下的子节点
/**//// <summary>
/// 删除XML节点和此节点下的子节点
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="Node">节点路径</param>
public static void XmlNodeDelete(string xmlPath, string Node)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
objXmlDoc.Save(xmlPath);
}
#endregion
删除一个节点的属性#region 删除一个节点的属性
/**//// <summary>
/// 删除一个节点的属性
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径(xpath)</param>
/// <param name="NodeAttribute">属性名称</param>
public static void xmlnNodeAttributeDel(string xmlPath, string NodePath, string NodeAttribute)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.RemoveAttribute(NodeAttribute);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一个节点和此节点的子节点#region 插入一个节点和此节点的子节点
/**//// <summary>
/// 插入一个节点和此节点的字节点
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="MailNode">当前节点路径</param>
/// <param name="ChildNode">新插入节点</param>
/// <param name="Element">插入节点的子节点</param>
/// <param name="Content">子节点的内容</param>
public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
向一个节点添加属性#region 向一个节点添加属性
/**//// <summary>
/// 向一个节点添加属性
/// </summary>
/// <param name="xmlPath">xml文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要添加的节点属性的名称</param>
/// <param name="NodeAttributeText">要添加属性的值</param>
public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一节点,带一属性#region 插入一节点,带一属性
/**//// <summary>
/// 插入一节点,带一属性
/// </summary>
/// <param name="xmlPath">Xml文档路径</param>
/// <param name="MainNode">当前节点路径</param>
/// <param name="Element">新节点</param>
/// <param name="Attrib">属性名称</param>
/// <param name="AttribContent">属性值</param>
/// <param name="Content">新节点值</param>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
插入一节点不带属性#region 插入一节点不带属性
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
在根节点下添加父节点#region 在根节点下添加父节点
/**//// <summary>
/// 在根节点下添加父节点
/// </summary>
public static void AddParentNode(string xmlPath,string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
// 创建一个新的menber节点并将它添加到根节点下
XmlElement Node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(Node);
xdoc.Save(xmlPath);
}
#endregion
//必须创建对象才能使用的类
private bool _alreadyDispose = false;
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode xmlNode;
private XmlElement xmlElem;
构造与释构#region 构造与释构
public XmlHelper()
{
}
~XmlHelper()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
//
}
_alreadyDispose = true;
}
#endregion
IDisposable 成员#region IDisposable 成员
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
创建xml文档#region 创建xml文档
创建一个只有声明和根节点的XML文档#region 创建一个只有声明和根节点的XML文档
/**//// <summary>
/// 创建一个只有声明和根节点的XML文档
/// </summary>
/// <param name="root"></param>
public void CreateXmlRoot(string root)
{
//加入XML的声明段落
xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlDoc.AppendChild(xmlNode);
//加入一个根元素
xmlElem = xmlDoc.CreateElement("", root, "");
xmlDoc.AppendChild(xmlElem);
}
#endregion
在当前节点下插入一个空节点节点#region 在当前节点下插入一个空节点节点
/**//// <summary>
/// 在当前节点下插入一个空节点节点
/// </summary>
/// <param name="mainNode">当前节点路径</param>
/// <param name="node">节点名称</param>
public void CreatXmlNode(string mainNode, string node)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
MainNode.AppendChild(objElem);
}
#endregion
在当前节点插入一个仅带值的节点#region 在当前节点插入一个仅带值的节点
/**//// <summary>
/// 在当前节点插入一个仅带值的节点
/// </summary>
/// <param name="mainNode">当前节点</param>
/// <param name="node">新节点</param>
/// <param name="content">新节点值</param>
public void CreatXmlNode(string mainNode, string node, string content)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
objElem.InnerText = content;
MainNode.AppendChild(objElem);
}
#endregion
在当前节点的插入一个仅带属性值的节点#region 在当前节点的插入一个仅带属性值的节点
/**//// <summary>
/// 在当前节点的插入一个仅带属性值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">新节点</param>
/// <param name="Attrib">新节点属性名称</param>
/// <param name="AttribValue">新节点属性值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
mainNode.AppendChild(objElem);
}
#endregion
创建一个带属性值的节点值的节点#region 创建一个带属性值的节点值的节点
/**//// <summary>
/// 创建一个带属性值的节点值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称</param>
/// <param name="AttribValue">属性值</param>
/// <param name="Content">节点传情</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
在当前节点的插入一个仅带2个属性值的节点#region 在当前节点的插入一个仅带2个属性值的节点
/**//// <summary>
/// 在当前节点的插入一个仅带2个属性值的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称一</param>
/// <param name="AttribValue">属性值一</param>
/// <param name="Attrib2">属性名称二</param>
/// <param name="AttribValue2">属性值二</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
mainNode.AppendChild(objElem);
}
#endregion
在当前节点插入带两个属性的节点#region 在当前节点插入带两个属性的节点
/**//// <summary>
/// 在当前节点插入带两个属性的节点
/// </summary>
/// <param name="MainNode">当前节点或路径</param>
/// <param name="Node">节点名称</param>
/// <param name="Attrib">属性名称一</param>
/// <param name="AttribValue">属性值一</param>
/// <param name="Attrib2">属性名称二</param>
/// <param name="AttribValue2">属性值二</param>
/// <param name="Content">节点值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2, string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
保存Xml#region 保存Xml
/**//// <summary>
/// 保存Xml
/// </summary>
/// <param name="path">保存的当前路径</param>
public void XmlSave(string path)
{
xmlDoc.Save(path);
}
#endregion
#endregion
读取指定节点的指定属性值#region 读取指定节点的指定属性值
/**//// <summary>
/// 功能:
/// 读取指定节点的指定属性值
/// </summary>
/// <param name="strNode">节点路径</param>
/// <param name="strAttribute">此节点的属性</param>
/// <returns></returns>
public string GetXmlNodeAttribute(string xmlPath, string strNode, string strAttribute)
{
string strReturn = "";
xmlDoc.Load(xmlPath);
try
{
//根据指定路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
//获取节点的属性,并循环取出需要的属性值
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for (int i = 0; i < xmlAttr.Count; i++)
{
if (xmlAttr.Item(i).Name == strAttribute)
strReturn = xmlAttr.Item(i).Value;
}
}
catch (XmlException xmle)
{
throw xmle;
}
return strReturn;
}
#endregion
读取指定节点的值#region 读取指定节点的值
/**//// <summary>
/// 功能:
/// 读取指定节点的值
/// </summary>
/// <param name="strNode">节点名称(xpath)</param>
/// <returns></returns>
public string GetXmlNodeValue(string xmlPath,string strNode)
{
string strReturn = String.Empty;
xmlDoc.Load(xmlPath);
try
{
//根据路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
strReturn = xmlNode.InnerText;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return strReturn;
}
#endregion
根据节点属性读取子节点值(较省资源模式)#region 根据节点属性读取子节点值(较省资源模式)
/**//// <summary>
/// 根据节点属性读取子节点值(较省资源模式)
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="FatherElement">父节点值</param>
/// <param name="AttributeName">属性名称</param>
/// <param name="AttributeValue">属性值</param>
/// <param name="ArrayLength">返回的数组长度</param>
/// <returns></returns>
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn;
xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
XmlNodeList xx = xn.Item(0).ChildNodes;
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{
al.Add(xx.Item(i).InnerText);
}
return al;
}
#endregion
}