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

来源:互联网 发布:淘宝的返利机器人 编辑:程序博客网 时间:2024/06/06 16:28
/// <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;
        }
0 0
原创粉丝点击