XML文件读取

看别人的代码觉得以后用的着,贴上来:
ExpandedBlockStart.gif代码
/// <summary>
        
/// 根据给定的配置文件中key获取其对应的value
        
/// </summary>
        
/// <param name="key">需要查找的key</param>
        
/// <returns>可以对应的value</returns>
        public string GetAppSettingValue(string key)
        {
            
//创建XML读取对象
            XmlDocument doc = new XmlDocument();
            
//获取XML文件的路径创建文件流读取器
            StreamReader _read = new StreamReader(GlobalCache.Instance.ConfigFile);
            
//加载XML文件
            doc.Load(_read);
            
//释放文件流读取器资源
            _read.Close();
            
//遍历XML文件查找所需节点的Value值
            foreach (XmlNode node in doc.DocumentElement["appSettings"])
            {
                
//查找节点,排除注释
                if (node.Attributes != null && string.Compare(key, node.Attributes["key"].Value) == 0)
                {
//返回key对应的value
                    return node.Attributes["value"].Value;
                }
            }
            
return "";
        }

 

文件路径的获取:
ExpandedBlockStart.gif代码
FileAccess fileAccess = new FileAccess();
                
//获取应用程序路径
                GlobalCache.Instance.AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
                    
+ @"\";
                
//获取配置文件路径
                GlobalCache.Instance.ConfigFile = GlobalCache.Instance.AppPath + @"Config\Appconfig.xml";

 

转载于:https://www.cnblogs.com/gjs85/archive/2010/03/06/1679544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值