我应该如何从 XML 文件中的 c# 中找到应用程序设置
How shall I find app setting from XML file in c#
我有一个 XML 如下:
<configuration>
<AppSettings>
<add key="FilePath" value="c:\test" />
<add key="domain" value="google" />
</AppSettings>
</configuration>
我想要一个快速的 c:\ 代码来从 .XML 文件中读取应用程序设置。我们不想使用 ConfigurationManager 和 App.config 路由,因为这些密钥是通过多个 projects/applications 共享的,因此要求将其保存在共享位置。
例如静态 class 方法如下:
settings.GetAppKey("FilePath") 应 return 值,例如c:\测试
添加 类 以反序列化,如下所示。
[XmlRoot(ElementName = "configuration")]
public class ApplicationConfiguration
{
private Dictionary<string, string> _appSettings = new Dictionary<string, string>();
[XmlArray(ElementName = "appSettings")]
[XmlArrayItem(ElementName = "add")]
public List<AppSetting> ToolsGroups { get; set; }
public string GetAppValue(string key)
{
return ToolsGroups.Where(x => x.Key.Equals(key)).FirstOrDefault().Value;
}
}
public class AppSetting
{
[XmlAttribute(AttributeName = "key")]
public string Key { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
然后反序列化使用
bool isAppConfigFormatCorrect = false;
string appConfigFile = Path.Combine(@"..\..\..\..\Config", "app.config");
ApplicationConfiguration appConfig = new ApplicationConfiguration();
try
{
XmlDocument xmlDocumentAppConfig = new XmlDocument();
xmlDocumentAppConfig.Load(appConfigFile);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ApplicationConfiguration));
XmlReader xmlReader = XmlReader.Create(appConfigFile);
appConfig = (ApplicationConfiguration)xmlSerializer.Deserialize(xmlReader);
isAppConfigFormatCorrect = true;
}
catch (Exception ec)
{
MessageBox.Show($"Structure of the '{appConfigFile}' file is incorrect." + Environment.NewLine + $"Error Message: {ec.Message}");
}
if (isAppConfigFormatCorrect)
{
Debug.Print(appConfig.GetAppValue("FilePath"));
}
我有一个 XML 如下:
<configuration>
<AppSettings>
<add key="FilePath" value="c:\test" />
<add key="domain" value="google" />
</AppSettings>
</configuration>
我想要一个快速的 c:\ 代码来从 .XML 文件中读取应用程序设置。我们不想使用 ConfigurationManager 和 App.config 路由,因为这些密钥是通过多个 projects/applications 共享的,因此要求将其保存在共享位置。
例如静态 class 方法如下: settings.GetAppKey("FilePath") 应 return 值,例如c:\测试
添加 类 以反序列化,如下所示。
[XmlRoot(ElementName = "configuration")]
public class ApplicationConfiguration
{
private Dictionary<string, string> _appSettings = new Dictionary<string, string>();
[XmlArray(ElementName = "appSettings")]
[XmlArrayItem(ElementName = "add")]
public List<AppSetting> ToolsGroups { get; set; }
public string GetAppValue(string key)
{
return ToolsGroups.Where(x => x.Key.Equals(key)).FirstOrDefault().Value;
}
}
public class AppSetting
{
[XmlAttribute(AttributeName = "key")]
public string Key { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
然后反序列化使用
bool isAppConfigFormatCorrect = false;
string appConfigFile = Path.Combine(@"..\..\..\..\Config", "app.config");
ApplicationConfiguration appConfig = new ApplicationConfiguration();
try
{
XmlDocument xmlDocumentAppConfig = new XmlDocument();
xmlDocumentAppConfig.Load(appConfigFile);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ApplicationConfiguration));
XmlReader xmlReader = XmlReader.Create(appConfigFile);
appConfig = (ApplicationConfiguration)xmlSerializer.Deserialize(xmlReader);
isAppConfigFormatCorrect = true;
}
catch (Exception ec)
{
MessageBox.Show($"Structure of the '{appConfigFile}' file is incorrect." + Environment.NewLine + $"Error Message: {ec.Message}");
}
if (isAppConfigFormatCorrect)
{
Debug.Print(appConfig.GetAppValue("FilePath"));
}