我正在开发一个能够通过datagridview操作xml文件的GUI,并将其保存到用户选择的目的地。这个程序还有一个.exe.config文件,我也希望能够在datagridview中自由编辑,因为它比用户手动进入文件并相应地更改值更方便。
我已经尝试声明了一个数据集,我最初认为.exe.config文件只是一个xml文件,但是这段代码不起作用:
dataSet1.ReadXml(configpath);
bindingSource1.DataSource = dataSet1.Tables[0];
dataGridView1.DataSource = bindingSource1;
当我运行它时,datagridview为空,我确认文件路径是正确的,并且在我调试代码时没有异常,而对于我在GUI中打开的其他xml文件,显示的数据完全正常。也许readxml()只支持..合法的xml文件而不是xml配置文件?我尝试谷歌搜索并寻找一些答案,但我得到的只是通过手动访问xml文件和更改值(我已经知道的东西)更改设置的线程。我希望能够让用户做他们想要做的数据,然后保存它。 .exe.config设置也可能适用于其他程序,但它本质上是一个xml配置文件。我认为网络上没有太多关于这个特殊问题的原因,因为设置通常是静态的,如果它们被更改,手动操作非常容易。
总结一下,
我正在寻找一种能够打开任何 .exe.config文件的方法,在datagridview中显示它,允许用户能够操作里面的数据值,然后保存文件,覆盖以前的数据设置。
感谢任何帮助。 提前谢谢!
编辑:我将上传我创建的xml文件的一个工作示例:我希望程序能够导航到.exe.config文件,然后打开它并让它显示如下设置名称是列,值是在datagridview的单元格中。不幸的是,我不能在我的家用电脑上做到这一点。
答案 0 :(得分:2)
这是我用来加载和操作配置文件的。您可能需要更改loadAppSettings
和loadConnStrings
方法以满足您的需求。
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace GenericManagementClasses
{
public class ConfigFile
{
private string m_ConfigFilePath;
private XmlDocument m_XmlDoc;
private FileStream fIn;
private StreamReader sr;
private StreamWriter sw;
private OrderedDictionary m_AppSettings;
private OrderedDictionary m_ConnectionStrings;
private XmlNode m_AppSettingsNode;
private XmlNode m_ConnectionStringsNode;
#region "Properties"
public String Path
{
get
{
return m_ConfigFilePath;
}
}
public OrderedDictionary AppSettings
{
get
{
return m_AppSettings;
}
}
public OrderedDictionary ConnectionStrings
{
get
{
return m_ConnectionStrings;
}
}
#endregion
#region "Constructors"
/// <summary>
/// Default constructor - declared private so that you can't instantiate an empty ConfigFile object
/// <code>ConfigFile cfg = new ConfigFile()</code> will result in a NotImplemented exception
/// </summary>
private ConfigFile()
{
throw new NotImplementedException("No default constructor for the ConfigFile class");
}
/// <summary>
/// Public constructor
/// <example>ConfigFile cfg = new ConfigFile(@"c:\MyApp\MyApp.exe.config");</example>
/// </summary>
/// <param name="ConfigFilePath">The path to the configuration file</param>
public ConfigFile(string ConfigFilePath)
{
//Check to see if the file exists
if (File.Exists(ConfigFilePath)){
//Initialise the XmlDocument to hold the config file
m_XmlDoc = new XmlDocument();
//Store the path to the config file
m_ConfigFilePath = ConfigFilePath;
//FileStream to get the contents out of the file
fIn = new FileStream(m_ConfigFilePath, FileMode.Open, FileAccess.ReadWrite);
//StreamReader to read the FileStream
sr = new StreamReader(fIn);
//StreamWriter to write to the FileStream
sw = new StreamWriter(fIn);
//Try and load the XML from the file stream
try
{
m_XmlDoc.LoadXml(sr.ReadToEnd());
m_AppSettingsNode = m_XmlDoc.GetElementsByTagName("appSettings")[0];
m_ConnectionStringsNode = m_XmlDoc.GetElementsByTagName("connectionStrings")[0];
loadAppSettings();
loadConnStrings();
}
catch (Exception ex)
{
//If it went pear shaped, throw the exception upwards
throw ex;
}
}
else
//If the file doesn't exist, throw a FileNotFound exception
{
throw new FileNotFoundException(ConfigFilePath + " does not exist");
}
}
#endregion
private void loadAppSettings()
{
m_AppSettings = new OrderedDictionary();
XmlNodeList nl = m_AppSettingsNode.SelectNodes("add");
foreach (XmlNode node in nl)
{
m_AppSettings.Add(node.Attributes["key"].Value, node.Attributes["value"].Value);
}
}
private void loadConnStrings()
{
m_ConnectionStrings = new OrderedDictionary();
XmlNodeList nl = m_ConnectionStringsNode.SelectNodes("add");
foreach (XmlNode node in nl)
{
m_ConnectionStrings.Add(node.Attributes["name"].Value, node.Attributes["connectionString"].Value);
}
}
public void setAppSetting(string name, string newValue)
{
if (!m_AppSettings.Contains(name))
{
throw new Exception(String.Format("Setting {0} does not exist in {1}", name, m_ConfigFilePath));
}
else
{
m_AppSettings[name] = newValue;
m_XmlDoc.SelectSingleNode(String.Format(@"//appSettings/add[@key='{0}']",name)).Attributes["value"].Value = newValue;
fIn.SetLength(0);
sw.Write(m_XmlDoc.InnerXml);
sw.Flush();
}
}
#region "Static Methods"
/// <summary>
/// Static method to return a ConfigFile object
/// <example>ConfigFile cfg = ConfigFile.LoadConfigFile(@c:\MyApp\MyApp.exe.config");"</example>
/// </summary>
/// <param name="ConfigFilePath">Path to the configuration file to load</param>
/// <returns></returns>
public static ConfigFile LoadConfigFile(string ConfigFilePath)
{
return new ConfigFile(ConfigFilePath);
}
#endregion
}
}