XML文件读取

看别人的代码觉得以后用的着,贴上来:
代码
/// <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 "";
        }

 

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

 

posted @ 2010-03-06 09:29  古史漫谈  阅读(303)  评论(0编辑  收藏  举报