How to serialize nested sorted dictionaires?

S

Siegfried Heintze

(1) Can someone help me make the program below work? Line 26 throws an
exception. Something about property not supported?
(2) I tried writing cons["a"]["b"] = "hello" and this did not work because
cons["a"] was not initalized. How can I implement the descendant class SD so
it will automatically initialize new entries in the table to mimic the C++
class std::map<K,V> where I can write cons["a"]["b"]="hello"?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; /* 5*/
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
namespace DeleteMe_Test004_2D_HashTables
{ /* 10*/
class SD<K,V> : System.Collections.Generic.SortedDictionary<K,V>
{ }
public class Program {
static System.IO.TextWriter outp = System.Console.Out;
public static void Main(string[] args){/* 15*/
try{
SD<string, SD<string, string>> cons
=new SD<string,SD<string,string>>();
cons.Add("a",new SD<string,string>());
cons["a"].Add("b", "hello"); /* 20*/
foreach (string src in cons.Keys)
foreach (string dst in cons[src].Keys)
outp.WriteLine("cons[" + src + "]"
+ "[" + dst + "]=" + cons[src][dst]);
/* 25*/
XmlSerializer sr = new XmlSerializer(
typeof(SD<string, SD<string, string>>));
sr.Serialize(outp, cons);
} finally {
} /* 30*/
}
}
}
 
F

Family Tree Mike

Siegfried said:
(1) Can someone help me make the program below work? Line 26 throws an
exception. Something about property not supported?
(2) I tried writing cons["a"]["b"] = "hello" and this did not work because
cons["a"] was not initalized. How can I implement the descendant class SD so
it will automatically initialize new entries in the table to mimic the C++
class std::map<K,V> where I can write cons["a"]["b"]="hello"?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; /* 5*/
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
namespace DeleteMe_Test004_2D_HashTables
{ /* 10*/
class SD<K,V> : System.Collections.Generic.SortedDictionary<K,V>
{ }
public class Program {
static System.IO.TextWriter outp = System.Console.Out;
public static void Main(string[] args){/* 15*/
try{
SD<string, SD<string, string>> cons
=new SD<string,SD<string,string>>();
cons.Add("a",new SD<string,string>());
cons["a"].Add("b", "hello"); /* 20*/
foreach (string src in cons.Keys)
foreach (string dst in cons[src].Keys)
outp.WriteLine("cons[" + src + "]"
+ "[" + dst + "]=" + cons[src][dst]);
/* 25*/
XmlSerializer sr = new XmlSerializer(
typeof(SD<string, SD<string, string>>));
sr.Serialize(outp, cons);
} finally {
} /* 30*/
}
}
}

Dictionaries are not XmlSerializable, so you need to either find someone
who has create a class that is, or impliment IXmlSerializable yourself.
After you do the latter, you will need to make your class public, and
provide a parameterless constructor. The former is probably easier.
Search for Serializable Dictionary CSharp and I'm sure you will find
something that works for you.
 

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

Top