I am trying to read a configuration file on my Netduino Plus 2 using the .NET Micro Framework. I have copied most of the code from this blog post by Marco Minerva Saving settings to XML Configuration Files the only difference is that I have another way of accessing my SD card.
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);
}
}
On the SD card is a file "Application.config" with the following content in 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>
The below code is breaking on row 3 with the following error: "An unhandled exception of type 'System.NotSupportedException' occurred in 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;
}
}
}
}
}
What have I missed?
Apparently this is not supported since XmlReader is to big to fit into the core of the Netduino. :-(