public Dictionary<string, string> ReadConfig(string xmlPath)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (XmlNode xnode in xmlDoc["configuration"]["appSettings"])
{
if (xnode.Name == "add")
{
dic.Add(xnode.Attributes["key"].Value, xnode.Attributes["value"].Value);
}
}
return dic;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 保存配置文件信息
/// </summary>
public bool SaveConfig(string xmlPath,Dictionary<string, string> dic)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNode node = xmlDoc.SelectSingleNode("//appSettings");
foreach (KeyValuePair<string, string> d in dic)
{
XmlElement elem = (XmlElement)node.SelectSingleNode("//add[@key='" + d.Key + "']");
if (elem != null)
{
elem.SetAttribute("value", d.Value);
}
else
{
elem = xmlDoc.CreateElement("add");
elem.SetAttribute("key", d.Key);
elem.SetAttribute("value", d.Value);
node.AppendChild(elem);
}
}
xmlDoc.Save(xmlPath);
return true;
}
catch (Exception ex)
{
return false;
}
}