RE: Complex data types in User-scoped ApplicationSettings
- From: wawang@xxxxxxxxxxxxxxxxxxxx (Walter Wang [MSFT])
- Date: Thu, 18 May 2006 07:41:12 GMT
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.
.
- Prev by Date: Re: show a windows form in front of an arbitrary fullscreen applicatio
- Next by Date: Problem passing List<T>
- Previous by thread: nulls versus ?
- Next by thread: Problem passing List<T>
- Index(es):
Relevant Pages
|