【工具类】-Xml配置文件读写操作类

本文介绍了一套针对XML文件进行读取、写入及节点值更新等操作的实用方法,包括获取XML路径、从XML中读取数据集、获取指定目录下的所有子节点值等。这些方法有助于简化XML文件处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class ManageXML  
    {  
        /// <summary>  
        /// 返回用户操作的SQL语句  
        /// </summary>  
        /// <param name="sqlNodeName">XML的操作节点</param>  
        /// <returns>操作的sql语句</returns>  
        public static string GetXMLPath(string strXMlFileName)  
        {  
            string m_strFullPath = "";  
            Assembly Asm = Assembly.GetExecutingAssembly();  
            //获取文件的路径                    
            //m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "XMLSql.xml";  
            m_strFullPath = AppDomain.CurrentDomain.BaseDirectory + "XMLLibrary\\" + strXMlFileName;  
            return m_strFullPath;  
        }  
        /// <summary>  
        /// 获取XML数据库中的数据的方法  
        /// </summary>  
        /// <param name="strFilePath">传入文件路径</param>  
        /// <returns>返回一个数据集</returns>  
        public static DataSet GetAllDataFromXML(string strFilePath)  
        {  
            DataSet ds = new DataSet();  
            FileInfo fileInfo = new FileInfo(strFilePath);  
            if (fileInfo.Exists)  
            {  
                try  
                {  
                    ds.ReadXml(strFilePath);  
                }  
                catch { }  
            }  
            else  
            {  
                ds = null;  
            }  
            if (ds != null)  
            {  
                if (ds.Tables[0].Rows.Count < 1)  
                    ds = null;  
            }  
            return ds;  
        }  
  
  
        /// <summary>  
        /// 获取指定目录下所有子节点的值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeDir">节点目录</param>  
        /// <returns></returns>  
        public static  Hashtable GetNodeList(string strFileName, string nodeDir)  
        {  
            Hashtable strNodeList = new Hashtable();  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
  
  
                foreach (XmlNode xn in nodeList)     //遍历所有子节点   
                {  
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
                    strNodeList.Add(xe.GetAttribute("id").ToString(), xe.InnerText.Trim());  
                }  
  
  
            }  
            catch (Exception)  
            {  
  
  
                throw;  
            }  
            return strNodeList;  
        }  
  
  
        /// <summary>  
        /// 获取指定节点的值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="value">设置后的值</param>  
        /// <param name="nodeDir">指定节点所在的节点目录</param>  
        /// <returns></returns>  
        public static string GetNodeValue(string strFileName, string nodeName, string nodeDir)  
        {  
            string value = null;  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
  
  
                foreach (XmlNode xn in nodeList)    //遍历所有子节点   
                {  
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
  
  
                    if (xe.Name == nodeName)  
                    {  
                        value = xe.InnerText.Trim();  
  
  
                        break;  
                    }  
                }  
            }  
            catch (Exception exp)  
            {  
                throw exp;  
            }  
  
  
            return value;  
        }  
  
  
        /// <summary>  
        /// 获取指定节点下面对应属性的值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="nodeDir">指定节点所在的节点目录</param>  
        /// <param name="attribute">节点对应的属性名称</param>  
        /// <returns></returns>  
        public static string GetNodeValue(string strFileName, string nodeName, string nodeDir, string attribute)  
        {  
            string value = null;  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
  
  
                foreach (XmlNode xn in nodeList)    //遍历所有子节点   
                {  
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
  
  
                    if (xe.Name == nodeName)  
                    {  
                        //value = xe.InnerText.Trim();  
                        value = (xe).Attributes[attribute].Value;  
                        break;  
                    }  
                }  
            }  
            catch (Exception exp)  
            {  
                throw exp;  
            }  
  
  
            return value;  
        }  
  
  
        /// <summary>  
        /// 修改指定结点值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="value">设置后的值</param>  
        /// <param name="nodeDir">指定节点所在的节点目录</param>  
        /// <returns></returns>  
        public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir)  
        {  
            bool isSucceed = false;  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
  
  
                foreach (XmlNode xn in nodeList)    //遍历所有子节点   
                {  
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
  
  
                    if (xe.Name == nodeName)  
                    {  
                        xe.InnerText = value;  
  
  
                        isSucceed = true;  
                        break;  
                    }  
                }  
  
  
                xmlDoc.Save(strFileName);  
            }  
            catch (Exception exp)  
            {  
                throw exp;  
            }  
  
  
            return isSucceed;  
        }  
  
  
        /// <summary>  
        /// 修改指定结点值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="value">设置后的值</param>  
        /// <param name="nodeDir">指定节点所在的节点目录</param>  
        /// <returns></returns>  
        public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir, string attribute, string attributeValue)  
        {  
            bool isSucceed = false;  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
  
  
                foreach (XmlNode xn in nodeList)    //遍历所有子节点   
                {  
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
  
  
                    if (xe.Name == nodeName)  
                    {  
                        xe.InnerText = value;  
                        (xe).Attributes[attribute].Value = attributeValue;  
                        isSucceed = true;  
                        break;  
                    }  
                }  
  
  
                xmlDoc.Save(strFileName);  
            }  
            catch (Exception exp)  
            {  
                throw exp;  
            }  
  
  
            return isSucceed;  
        }  
  
  
  
  
        /// <summary>  
        /// 修改指定结点值  
        /// </summary>  
        /// <param name="strFileName">文件路径</param>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="value">设置后的值</param>  
        /// <param name="nodeDir">指定节点所在的节点目录</param>  
        /// <returns></returns>  
        public static bool UpdateNoteValue(string strFileName, Hashtable hstable, string nodeDir)  
        {  
            bool isSucceed = false;  
            try  
            {  
                XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load(strFileName);  
  
  
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点   
                foreach (DictionaryEntry item in hstable)  
                {  
                    foreach (XmlNode xn in nodeList)    //遍历所有子节点   
                    {  
                        XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型   
  
  
                        if (xe.Name == item.Key.ToString())  
                        {  
                            xe.InnerText = item.Value.ToString();  
  
  
                            isSucceed = true;  
                            break;  
                        }  
                    }  
                }  
  
  
                xmlDoc.Save(strFileName);  
            }  
            catch (Exception exp)  
            {  
                throw exp;  
            }  
  
  
            return isSucceed;  
        }  
  
}   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值