XML操作C#代码
using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace NetProject.Property { public class XmlDoc { /// <summary> /// 创建Xml文件 /// </summary> /// <param name="xmlPath">创建文件路径</param> /// <param name="element"></param> public void CreateXmlNode(string xmlPath, string element) { //实例化Xml文档类 XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath,Encoding.UTF8); //创建Xml声明 xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement(element); xmlTextWriter.Flush(); xmlTextWriter.Close(); } /// <summary> /// 新增Xml节点 /// </summary> /// <param name="xmlPath"></param> /// <param name="singleNode"></param> /// <param name="node"></param> public void AddXmlElement(string xmlPath, string singleNode, XmlNode node) { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(xmlPath); xmlDocument.SelectSingleNode(singleNode).AppendChild(node); xmlDocument.Save(xmlPath); } /// <summary> /// 新增Xml节点 /// </summary> /// <param name="xmlPath"></param> /// <param name="singleNode"></param> /// <param name="node"></param> public void AddXmlElement(string xmlPath, string singleNode, string element, string attribute, string value) { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(xmlPath); XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode); XmlElement xmlElement = xmlDocument.CreateElement(element); ; xmlElement.SetAttribute(attribute, value); xmlDocument.Save(xmlPath); } /// <summary> /// 读取XML文件的XmlDocument /// </summary> /// <param name="xmlPath">Xml文件路径</param> /// <returns></returns> public XmlDocument GetXmlDocument(string xmlPath) { try { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(GetMapPath(xmlPath)); return xmlDocument; } catch (System.Xml.XmlException exp) { throw exp; } catch (System.IO.IOException exp) { throw exp; } catch (ArgumentNullException exp) { throw exp; } catch (ArgumentException exp) { throw exp; } catch (Exception exp) { throw exp; } } /// <summary> /// 读取XML文件的XmlNode /// </summary> /// <param name="xmlPath">Xml文件路径</param> /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param> /// <returns></returns> public XmlNode GetXmlNode(string xmlPath, string singleNode) { try { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(GetMapPath(xmlPath)); //获取文件内容 return xmlDocument.SelectSingleNode(singleNode); } catch (System.Xml.XPath.XPathException exp) { throw exp; } catch (System.Xml.XmlException exp) { throw exp; } catch (System.IO.IOException exp) { throw exp; } catch (ArgumentNullException exp) { throw exp; } catch (ArgumentException exp) { throw exp; } catch (Exception exp) { throw exp; } } /// <summary> /// 读取XML文件的XmlElement /// </summary> /// <param name="xmlPath">Xml文件路径</param> /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param> /// <param name="xmlNodeString">节点名称</param> /// <returns></returns> public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString) { try { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(GetMapPath(xmlPath)); //获取文件内容 XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode); //提取节点名称下的属性值 return xmlNode[xmlNodeString]; } catch (System.Xml.XPath.XPathException exp) { throw exp; } catch (System.Xml.XmlException exp) { throw exp; } catch (System.IO.IOException exp) { throw exp; } catch (ArgumentNullException exp) { throw exp; } catch (ArgumentException exp) { throw exp; } catch (Exception exp) { throw exp; } } /// <summary> /// 读取XML文件的节点属性 /// </summary> /// <param name="xmlPath">Xml文件路径</param> /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param> /// <param name="xmlNodeString">节点名称</param> /// <param name="variables">节点属性</param> /// <returns></returns> public string GetAttribute(string xmlPath, string singleNode, string xmlNodeString, string variables) { try { //实例化Xml文档类 XmlDocument xmlDocument = new XmlDocument(); //加载Xml文件 xmlDocument.Load(GetMapPath(xmlPath)); //获取文件内容 XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode); //提取节点名称下的属性值 return xmlNode[xmlNodeString].GetAttribute(variables); } catch (System.Xml.XPath.XPathException exp) { throw exp; } catch (System.Xml.XmlException exp) { throw exp; } catch (System.IO.IOException exp) { throw exp; } catch (ArgumentNullException exp) { throw exp; } catch (ArgumentException exp) { throw exp; } catch (Exception exp) { throw exp; } } /// <summary> /// 设置XML节点 /// </summary> /// <param name="xmlNodeString">节点名称</param> /// <param name="value">节点值</param> /// <param name="discrption">节点说明</param> public void SetAttribute(string xmlPath, string singleNode,string xmlNodeString, string attribute,string value,string description) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(GetMapPath(xmlPath)); //获取文件内容 XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode); //设置指定XML节点Value值 xmlNode[xmlNodeString].SetAttribute(attribute, value); xmlNode[xmlNodeString].SetAttribute("Description", description); //将设置后的XML节点保存 xmlDocument.Save(GetMapPath(xmlPath)); } catch (Exception exp) { throw new Exception(exp.Message); } } /// <summary> /// 设置XML节点 /// </summary> /// <param name="xmlPath">XML文件路径</param> /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param> /// <param name="xmlNodeString">指定节点</param> /// <param name="attribute">属性数组</param> /// <param name="value">值数组</param> public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string[] attribute, string[] value) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(GetMapPath(xmlPath)); //获取文件内容 XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode); for (int i = 0; i < attribute.Length; i++) { //设置指定XML节点Value值 xmlNode[xmlNodeString].SetAttribute(attribute[i], value[i]); } //将设置后的XML节点保存 xmlDocument.Save(xmlPath); } catch (Exception exp) { throw new Exception(exp.Message); } } /// <summary> /// 通过XML名称获取XML物理路径,可无限添加,前提是XML都位于同一网站目录下 /// </summary> /// <param name="xmlName"></param> /// <returns></returns> private string GetMapPath(string xmlName) { switch (xmlName) { case "GlobalVariables.xml": xmlName = System.Web.HttpContext.Current.Server.MapPath("~/XML/") + xmlName; break; default: break; } return xmlName; } } }