【发布时间】:2012-12-17 21:55:12
【问题描述】:
我正在尝试使用 .NET Micro Framework 在我的 Netduino Plus 2 上读取配置文件。 我已经从 Marco Minerva 的这篇博文中复制了大部分代码 Saving settings to XML Configuration Files 唯一的区别是我有另一种方式访问我的 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;
}
}
}
}
}
我错过了什么?
【问题讨论】:
-
有内部异常吗?
-
在 xml 文件的第 3 行中断? APPSETTINGS_SECTION 等于什么。
-
@Sorceri 对不起,错过了一些代码。该帖子现已更新。中断发生在以下行: using (XmlReader reader = XmlReader.Create(xmlStream))
-
@Cameron 没有内部异常,我只在 Visual Studio 中弹出一个带有中断或继续选项的窗口。
标签: c# .net xmlreader .net-micro-framework netduino