C#获取和更改xml文件(配置文件)

/// <summary>
        /// 修改指定结点值
        /// </summary>
        /// <param name="strFileName">文件路径</param>
        /// <param name="nodeName">节点名称</param>
        /// <param name="value">设置后的值</param>
        /// <param name="nodeDir">指定节点所在的节点目录</param>
        /// <returns></returns>
        public static bool UpdateGSTCDValue(/*string strFileName, */string nodeName, string gstcdValue)
        {
            bool isSucceed = false;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                str = str.Substring(0, str.Length - 10) + "App.config";
                xmlDoc.Load(str);

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点

                foreach (XmlNode xn in nodeList)    //遍历所有子节点
                {
                    //判断如果是注释则过滤掉
                    if (xn is XmlComment)
                    {
                        continue;
                    }
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

                    if (xe.Name == nodeName)
                    {
                        xe.SetAttribute("GSTCDMAX", gstcdValue);
                        isSucceed = true;
                        break;
                    }
                }

                xmlDoc.Save(str);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return isSucceed;
        }

        public static bool UpdateSSTCDValue(/*string strFileName, */string nodeName, string sstcdValue)
        {
            bool isSucceed = false;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                str = str.Substring(0, str.Length - 10) + "App.config";
                xmlDoc.Load(str);

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点

                foreach (XmlNode xn in nodeList)    //遍历所有子节点
                {
                    //判断如果是注释则过滤掉
                    if (xn is XmlComment)
                    {
                        continue;
                    }
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

                    if (xe.Name == nodeName)
                    {
                        xe.SetAttribute("SSTCDMAX", sstcdValue);
                        isSucceed = true;
                        break;
                    }
                }

                xmlDoc.Save(str);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return isSucceed;
        }


        /// <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 nodeName)
        {
            string value = null;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                str = str.Substring(0, str.Length - 10) + "App.config";
                xmlDoc.Load(str);

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点

                foreach (XmlNode xn in nodeList)    //遍历所有子节点
                {
                    //判断如果是注释则过滤掉
                    if (xn is XmlComment)
                    {
                        continue;
                    }
                    XmlElement xe = (XmlElement)xn;  //将子节点类型转换为XmlElement类型

                    if (xe.Name == nodeName)
                    {
                        //value = xe.InnerText.Trim();
                        value = (xe).Attributes["STTP"].Value;
                        break;
                    }
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }

            return value;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值