我正在尝试使用 .NET Micro Framework 在我的 Netduino Plus 2 上读取配置文件。我已经从 Marco Minerva 的这篇博文中复制了大部分代码,将 设置保存到 XML 配置文件,唯一的区别是我有另一种访问 SD 卡的方式。
private static void LoadConfiguration()
{
const string filePath = @"SD\\Application.config";
using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
ConfigurationManager.Load(stream);
}
}
SD 卡上有一个“Application.config”文件,其内容为 UTF8。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key1" value="Value1" />
<add key="Key2" value="Value2" />
<add key="Key3" value="Value3" />
<add key="hostName" value="www.google.it" />
<add key="port" value="25" />
<add key="randomkey" value="randomvalue"/>
</appSettings>
</configuration>
下面的代码在第 3 行中断并出现以下错误: “System.Xml.dll 中发生了 'System.NotSupportedException' 类型的未处理异常”
public static class ConfigurationManager
{
private const string APPSETTINGS_SECTION = "appSettings";
private const string ADD = "add";
private const string KEY = "key";
private const string VALUE = "value";
private static Hashtable appSettings;
static ConfigurationManager()
{
appSettings = new Hashtable();
}
public static string GetAppSetting(string key)
{
return GetAppSetting(key, null);
}
public static string GetAppSetting(string key, string defaultValue)
{
if (!appSettings.Contains(key))
return defaultValue;
return (string)appSettings[key];
}
public static void Load(Stream xmlStream)
{
using (XmlReader reader = XmlReader.Create(xmlStream))
{
while (reader.Read())
{
switch (reader.Name)
{
case APPSETTINGS_SECTION:
while (reader.Read())
{
if (reader.Name == APPSETTINGS_SECTION)
break;
if (reader.Name == ADD)
{
var key = reader.GetAttribute(KEY);
var value = reader.GetAttribute(VALUE);
//Debug.Print(key + "=" + value);
appSettings.Add(key, value);
}
}
break;
}
}
}
}
}
我错过了什么?