操作xml文件类

来源:互联网 发布:大数据属于什么行业 编辑:程序博客网 时间:2024/05/21 21:33
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Collections;
using System.Windows.Forms;

namespace OperateXML
{
    /// <summary>
    /// 操作xml文件
    /// </summary>
    public class OperateXML
    {
        private static OperateXML operate = null;
        private static XmlDocument _xmlDoc = null;
        private static string _path = string.Empty;

        private OperateXML(string path)
        {
            _path = path;
            _xmlDoc = new XmlDocument();
            if (_path != string.Empty)
                _xmlDoc.Load(path);
        }
        /// <summary>
        /// path为空时 表示创建XMLFile
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static OperateXML Instance(string path)
        {
            if (operate == null)
                operate = new OperateXML(path);
            return operate;
        }

        /// <summary>
        /// 初始化xml文件
        /// </summary>
        public static void GenerateXMLFile(string savePath,string rootName,DataTable dt,string childsName,string[] childs)
        {
            try
            {
                XmlNode xmlDeclaration = _xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                _xmlDoc.AppendChild(xmlDeclaration);
                //根节点
                XmlElement rootElement = _xmlDoc.CreateElement(rootName);
                _xmlDoc.AppendChild(rootElement);
                foreach (DataRow dr in dt.Rows)
                {
                    XmlElement moduesElement = _xmlDoc.CreateElement(childsName);
                    rootElement.AppendChild(moduesElement);

                    GenerateXMLChildNode(moduesElement, childs, dr);
                }
                _xmlDoc.Save(savePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (File.Exists(savePath))
                    File.Delete(savePath);
            }
        }
        /// <summary>
        /// 追加子节点
        /// </summary>
        /// <param name="fElement"></param>
        /// <param name="childs"></param>
        /// <param name="dr"></param>
        public static void GenerateXMLChildNode(XmlElement fElement,string[] childs,DataRow dr)
        {
            foreach (string item in childs)
            {
                XmlElement childNode = _xmlDoc.CreateElement(item);
                childNode.InnerText = dr[item].ToString();
                fElement.AppendChild(childNode);
            }
        }

        public static void GenerateXMLChildNode(XmlElement fElement,Hashtable ht)
        {
            foreach (string item in ht.Keys)
            {
                XmlElement childNode = _xmlDoc.CreateElement(item);
                childNode.InnerText = ht[item].ToString();
                fElement.AppendChild(childNode);
            }
        }
        /// <summary>
        /// 插入一个节点,带一属性。
        /// </summary>
        /// <param name="fElement"></param>
        /// <param name="element"></param>
        /// <param name="attrib"></param>
        /// <param name="attribContent"></param>
        /// <param name="content"></param>
        public static void GenerateXMLChildNode(XmlElement fElement, string element, string attrib, string attribContent, string content)
        {
            XmlElement childNode = _xmlDoc.CreateElement(element);
            childNode.SetAttribute(attrib, attribContent);
            childNode.InnerText = content;
            fElement.AppendChild(childNode);
        }

        /// <summary>
        /// 获取节点集合
        /// </summary>
        /// <param name="xPath">/root/modues</param>
        /// <returns></returns>
        public static XmlNodeList Read(string xPath)
        {
            XmlNodeList returnNodeList = null;
            try
            {
                returnNodeList = _xmlDoc.SelectNodes(xPath);
            }
            catch { }
            return returnNodeList;
        }
        /// <summary>
        /// 获得datatable
        /// </summary>
        /// <param name="xPath"> /Employee/@id 或者 Company/Department/Department_Name | Company/Department/Manager</param>
        /// <returns></returns>
        public static DataTable ReadToDT(string xPath)
        {
            DataSet ds = new DataSet();
            try
            {
                StringReader read = new StringReader(_xmlDoc.SelectSingleNode(xPath).OuterXml);
                ds.ReadXml(read);
            }
            catch { }
            return ds.Tables[0];
        }
        /// <summary>
        /// 根据子节点值获取此节点
        /// </summary>
        /// <param name="fNode"></param>
        /// <param name="xPath"> "modues/fid[text()='" + findValue + "']" 或者 Deparmt_Name/text()</param>
        /// <returns></returns>
        public static XmlNode ReadByText(XmlNode fNode,string xPath)
        {
            XmlNode returnnode = null;
            try
            {
                returnnode = _xmlDoc.SelectSingleNode(xPath);
            }
            catch { }
            return returnnode;
        }
        /// <summary>
        /// 根据节点属性值获取节点
        /// </summary>
        /// <param name="fNode"></param>
        /// <param name="xPath"> Book/Authors[ISBN=\"0002\"] 或者 Employee/@id='22345']/Department_Name</param>
        /// <returns></returns>
        public static XmlNode ReadByAttribute(XmlNode fNode, string xPath)
        {
            XmlNode returnnode = null;
            try
            {
                returnnode = _xmlDoc.SelectSingleNode(xPath).ParentNode;
            }
            catch { }
            return returnnode;
        }
        /// <summary>
        /// 修改节点值
        /// </summary>
        /// <param name="xPath">Name[text()='Zhang Qi'] </param>
        /// <param name="value"></param>
        public static void UpdateNode(string xPath, string value)
        {
            _xmlDoc.SelectSingleNode(xPath).InnerText = value;
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="xPath">Employee[@id='10102']</param>
        public static void DeleteNode(string xPath)
        {
            string mainPath = xPath.Substring(0, xPath.LastIndexOf("/"));
            _xmlDoc.SelectSingleNode(mainPath).RemoveChild(_xmlDoc.SelectSingleNode(xPath));
        }
    }
}