Complex data types in User-scoped ApplicationSettings

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How are complex data types saved and restored when used as User-scoped
Application Settings? For an example of what I'm trying to do that does not
work, take the following:

A project has a user-scoped Settings object called "TestStrings" which is of
type "StringDictionary". The following code is run:

if (Settings.Default.TestStrings == null)
Settings.Default.TestStrings = new StringDictionary();
Settings.Default.TestStrings.Add("Test", "Value");
Settings.Default.Save();

Stepping through that code reveals that at each line, the proper action is
performed successfully -- TestStrings is instantiated as a new dictionary,
and a new KeyValuePair is added to it. But each time this program is run,
TestString is *always* null. Other string variables stored in the same
Settings instance are loaded and saved correctly, but TestStrings is not.

Are there any special tricks to dealing with complex objects in
ApplicationSettings?
 
Hi,

Thank you for posting!

The problem that StringDictionary type settings is not saved is because it
doesn't have a TypeConverter and not xml serializable.

There are two primary mechanisms that ApplicationSettingsBase uses to
serialize settings:
1) If a TypeConverter exists that can convert to and from string, we use it.
2) If not, we fallback to the XmlSerializer.

Here we can derive a custom type from StringDictionary and either implement
a TypeConverter for it or implement IXmlSerializable.

Following is a sample which implements IXmlSerializable:

public class SerializableStringDictionary : StringDictionary,
IXmlSerializable
{

#region Node class
[Serializable]
public class Node
{
public Node()
{ }

public Node(string k, string v)
{
key = k;
val = v;
}

public string key;
public string val;
}

#endregion Node class for XML Serialization

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList), new System.Type[] {
typeof(Node) });

reader.Read();
ArrayList list = x.Deserialize(reader) as ArrayList;

if (list == null)
return;

foreach (Node node in list)
{
Add(node.key, node.val);
}
}

public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList), new System.Type[] {
typeof(Node) });
ArrayList list = new ArrayList();
foreach (string key in this.Keys)
{
list.Add(new Node(key, this[key]));
}
x.Serialize(writer, list);
}

#endregion
}


Hope this helps.

Regards,

Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I'am have the same problem.

Do us have the example in VB.NET or have other solution?

I need urgent this.

Best Regards.
______________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top