我试图使用.NET微框架在Netduino Plus 2上读取一个配置文件。我从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.NotSupportedException类型的未处理异常发生在System.Xml.dll中”
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;
}
}
}
}}
我错过了什么?
发布于 2012-12-18 08:37:58
显然,这是不支持的,因为XmlReader太大,适合于Netduino的核心。:-(
Netduino论坛
https://stackoverflow.com/questions/13922859
复制相似问题