CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

读取XML配置文件的问题

楼主Cerulean_ZC(张诚)2006-05-04 14:47:20 在 .NET技术 / C# 提问

我将下面的代码生成一个DLL文件,别的项目通过引用该DLL文件读写XML配置文件,但可以写,但读出来是空串。请大家帮帮忙,谢谢。  
  using   System;  
  using   System.Xml;      
  using   System.Configuration;  
  using   System.Reflection;  
   
   
  public   class   ConfigSettings  
  {  
  private   ConfigSettings()  
  {  
  }  
   
  public   static   string   ReadSetting(string   key)  
  {  
  return   ConfigurationSettings.AppSettings[key];  
  }  
   
  public   static   void   WriteSetting(string   key,   string   value)  
  {  
  //   load   config   document   for   current   assembly  
  XmlDocument   doc   =   loadConfigDocument();  
   
  //   retrieve   appSettings   node  
  XmlNode   node   =     doc.SelectSingleNode("//appSettings");  
   
  if   (node   ==   null)  
  throw   new   InvalidOperationException("appSettings   section   not   found   in   config   file.");  
   
  try  
  {  
  //   select   the   'add'   element   that   contains   the   key  
  XmlElement   elem   =   (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']",   key));  
   
  if   (elem   !=   null)  
  {  
  //   add   value   for   key  
  elem.SetAttribute("value",   value);  
  }  
  else  
  {  
  //   key   was   not   found   so   create   the   'add'   element    
  //   and   set   it's   key/value   attributes    
  elem   =   doc.CreateElement("add");  
  elem.SetAttribute("key",   key);  
  elem.SetAttribute("value",   value);    
  node.AppendChild(elem);  
  }  
  doc.Save(getConfigFilePath());  
  }  
  catch  
  {  
  throw;  
  }  
  }  
   
  public   static   void   RemoveSetting(string   key)  
  {  
  //   load   config   document   for   current   assembly  
  XmlDocument   doc   =   loadConfigDocument();  
   
  //   retrieve   appSettings   node  
  XmlNode   node   =     doc.SelectSingleNode("//appSettings");  
   
  try  
  {  
  if   (node   ==   null)  
  throw   new   InvalidOperationException("appSettings   section   not   found   in   config   file.");  
  else  
  {  
  //   remove   'add'   element   with   coresponding   key  
  node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']",   key)));  
  doc.Save(getConfigFilePath());  
  }  
  }  
  catch   (NullReferenceException   e)  
  {  
  throw   new   Exception(string.Format("The   key   {0}   does   not   exist.",   key),   e);  
  }  
  }  
   
  private   static   XmlDocument   loadConfigDocument()  
  {  
  XmlDocument   doc   =   null;  
  try  
  {  
  doc   =   new   XmlDocument();  
  doc.Load(getConfigFilePath());  
  return   doc;  
  }  
  catch   (System.IO.FileNotFoundException   e)  
  {  
  throw   new   Exception("No   configuration   file   found.",   e);  
  }  
  }  
   
  private   static   string   getConfigFilePath()  
  {  
  return   Assembly.GetExecutingAssembly().Location   +   ".config";  
  }  
  } 问题点数:100、回复次数:8Top

1 楼yurow(路漫漫其修远兮,吾将上下而爬楼梯!)回复于 2006-05-04 23:59:27 得分 5

可以写?读的是空串?  
  public   static   string   ReadSetting(string   key)  
  {  
  return   ConfigurationSettings.AppSettings[key];  
  }  
   
  你把static   属性去掉试试Top

2 楼Cerulean_ZC(张诚)回复于 2006-05-05 00:14:33 得分 0

去掉后别的类就不能直接调用了。我有一个问题,再调用ConfigSettings.ReadSetting(key)时,这个ConfigSettings类是怎么知道去调用哪个文件的?Top

3 楼yurow(路漫漫其修远兮,吾将上下而爬楼梯!)回复于 2006-05-05 00:23:34 得分 50

ConfigSettings只识别当前应用程序目录的   web.config文件(我是这么认为的)Top

4 楼hdt(倦怠)回复于 2006-05-05 01:20:48 得分 5

("//appSettings")  
  ===>  
  ("/appSettings")Top

5 楼misvcom(零下一度)回复于 2006-05-05 01:22:54 得分 10

你写的话确认是写到DLL的那个配置文件里面了吗?  
   
  是不是写到调用DLL那个项目的配置文件里面了Top

6 楼Cerulean_ZC(张诚)回复于 2006-05-06 01:14:30 得分 0

是别的项目程序调用这个项目(生成的DLL,和原项目在同一解决方案里)XML配置文件哪个目录下都拷了一个。都不行,只是能写不能读。把文件名改为web.config也不行。Top

7 楼zhzuo(秋枫)回复于 2006-05-06 11:46:38 得分 30

请验证一下是不是Cache的原因?  
  public   static   string   ReadSetting(string   key)  
  {  
  return   ConfigurationSettings.AppSettings[key];  
  }  
  上面这条除了第一次读文件外,接下去的n次会读缓存。(Web.config除外,自己刷新)。  
  你试着使用XmlDocument来加载xml读取数据,是否能读到修改后的数据。  
  在vs   2005下面提供了新的ConfigurationManager类和Configuration类,实现你上面的功能易如反掌。  
  Top

8 楼Cerulean_ZC(张诚)回复于 2006-05-09 12:34:10 得分 0

可这个从来没读出来过,包括第一次.不知道怎么回事.XmlDocument来加载xml读取数据我不太会.小弟刚学C#,能具体给个代码吗?谢了Top

相关问题

关键词

得分解答快速导航

  • 帖主:Cerulean_ZC
  • yurow
  • yurow
  • hdt
  • misvcom
  • zhzuo

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo