首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XmlReader失败

XmlReader失败
EN

Stack Overflow用户
提问于 2012-12-17 21:55:12
回答 1查看 907关注 0票数 1

我试图使用.NET微框架在Netduino Plus 2上读取一个配置文件。我从Minerva 将设置保存到XML配置文件的博客文章中复制了大部分代码,唯一的区别是我有另一种方式访问我的SD卡。

代码语言:javascript
复制
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中的以下内容。

代码语言:javascript
复制
<?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中”

代码语言:javascript
复制
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;
            }
        }
    }
}

}

我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-18 08:37:58

显然,这是不支持的,因为XmlReader太大,适合于Netduino的核心。:-(

Netduino论坛

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13922859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档