1

.NETMicroFrameworkを使用してNetduinoPlus2の構成ファイルを読み取ろうとしています。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カードには、UTF8に次の内容が含まれるファイル「Application.config」があります。

<?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;
            }
        }
    }
}

}

私は何を逃しましたか?

4

1 に答える 1

0

XmlReader は大きすぎて Netduino のコアに収まらないため、明らかにこれはサポートされていません。:-(

Netduino フォーラム

于 2012-12-18T08:37:58.923 に答える