By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,210 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,210 IT Pros & Developers. It's quick & easy.

Complex data types in User-scoped ApplicationSettings

P: n/a
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?

May 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

May 18 '06 #2

P: n/a
I'am have the same problem.

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

I need urgent this.

Best Regards.
______________________________________

"Walter Wang [MSFT]" wrote:
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.

Jul 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.