简单的XML的读取修改

假如一个xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来 
     配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“ASP.NET 配置”
     选项。
     设置和注释的完整列表在
     machine.config.comments 中,该文件通常位于 \Windows\Microsoft.NET\Framework\v2.0.xxxxx\Config 中
 -->
<configuration>
  <appSettings>
    <add key="StartupFileWatch" value="True" />
    <add key="MaxSize" value="2097152" />
    <add key="MinSize" value="10" />
    <add key="WatchDir" value="UpLoadFolder" />
    <add key="IgnorePostfix" value=".html;.htm;.js" />
    <add key="CsCommand" value="1" />
    <add key="PeCommand" value="1" />
  </appSettings>
</configuration>
/// <summary>
        /// 是否启动实时监控
        /// </summary>
        static public bool StartupFileWatch
        {
            get
            {
                string str = XML.GetConfig("StartupFileWatch", @"Config\TopWinCMS.FileWatcher.config");
                return Convert.ToBoolean(str);
            }
            set
            {
                XML.SaveSetting("StartupFileWatch", value.ToString(), @"Config\TopWinCMS.FileWatcher.config");
            }
        }

/// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName"></param>
        /// <returns></returns>
        public static string GetConfig(string strKeyName)
        {
            return GetConfig(strKeyName, "Web.config");
        }

        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName">节点键名</param>
        /// <returns>指定节点键值</returns>
        public static string GetConfig(string strKeyName, string strWhich)
        {
            string configpath;
            try
            {
                configpath = string.Concat(System.Web.HttpRuntime.AppDomainAppPath, @"\", strWhich);
            }
            catch
            {
                configpath = System.Windows.Forms.Application.StartupPath;

                if (configpath.EndsWith(@"\bin"))
                {
                    configpath = configpath.Substring(0, configpath.Length - 4);
                }
                configpath = string.Concat(configpath, @"\", strWhich);
            }

            using (XmlTextReader tr = new XmlTextReader(configpath))
            {
                while (tr.Read())
                {
                    if (tr.NodeType == XmlNodeType.Element)
                    {
                        if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
                        {
                            return tr.GetAttribute("value");
                        }
                    }
                }
            }
            return null;
        }

/// <summary>
        /// 保存Web.config
        /// </summary>
        /// <param name="strKeyName">节点名</param>
        /// <param name="strKeyValue">要保存的值</param>
        /// <param name="strWhich">哪个配置文件</param>
        public static void SaveSetting(string strKeyName, string strKeyValue, string strWhich)
        {
            //创建XML文档实例
            System.Xml.XmlDocument XMLWebSetting = new System.Xml.XmlDocument();

            //打开XML文档
            XMLWebSetting.Load(System.Web.HttpRuntime.AppDomainAppPath + "/" + strWhich);

            //查找节点所在位置
            System.Xml.XmlNodeList XmlNodeList = XMLWebSetting.SelectSingleNode("//appSettings").ChildNodes;

            foreach (System.Xml.XmlNode xn in XmlNodeList)
            {
                if (xn.Attributes["key"].InnerText == strKeyName)
                {
                    xn.Attributes["value"].InnerText = strKeyValue;
                    XMLWebSetting.Save(System.Web.HttpContext.Current.Server.MapPath("~/" + strWhich + ""));
                    break;
                }
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值