RE: Complex data types in User-scoped ApplicationSettings

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.

.



Relevant Pages