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?"";
????????}
????????????????//獲取應用程序路徑
????????????????GlobalCache.Instance.AppPath?=?Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
????????????????????+?@"\";
????????????????//獲取配置文件路徑
????????????????GlobalCache.Instance.ConfigFile?=?GlobalCache.Instance.AppPath?+?@"Config\Appconfig.xml";
????????///?根據給定的配置文件中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";
?
轉載于:https://www.cnblogs.com/gjs85/archive/2010/03/06/1679544.html
總結
- 上一篇: IT服务台的进化(3)--虚拟服务台
- 下一篇: 操作系统回忆录:进程、线程、资源调度