我将下面的代码生成一个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";
}
}
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";
}
}
public static string ReadSetting(string key)
{
return ConfigurationSettings.AppSettings[key];
}你把static 属性去掉试试
===>
("/appSettings")
public static string ReadSetting(string key)
{
return ConfigurationSettings.AppSettings[key];
}
上面这条除了第一次读文件外,接下去的n次会读缓存。(Web.config除外,自己刷新)。
你试着使用XmlDocument来加载xml读取数据,是否能读到修改后的数据。
在vs 2005下面提供了新的ConfigurationManager类和Configuration类,实现你上面的功能易如反掌。