serialize sorteddictionary-does this seem ok

M

mp

does this seem a reasonable way to serialize a sorted dictionary?
it works which is cool, but are there issues?
calling code:
sdictCriteriaList_Selected is a SortedDictionary<string,string>
private void btnStoreCriteria_Click(object sender, EventArgs e)
{
CriteriaStore cs = new
CriteriaStore(sdictCriteriaList_Selected);
cs.Store();
}

private void btnRetrieveCriteria_Click(object sender, EventArgs e)
{
SortedDictionary<string, string> sdReadBack;
CriteriaStore cs = new CriteriaStore();
sdReadBack = cs.Retrieve();
//just to see that it's working...
foreach (KeyValuePair<string, string> kvp in sdReadBack)
{ Debug.Print("key " + kvp.Key + " value " + kvp.Value); }
}

the class
[Serializable()]
public class Criteria
{
public string Key { get; set; }
public string Value { get; set; }

public Criteria(string key, string value)
{
Key = key;
Value=value;}

}
class CriteriaStore
{
private SortedDictionary<string, string> _criteriaDictionary;
public CriteriaStore()//to retrieve don't need input arg
{}

public CriteriaStore(SortedDictionary <string,string>
criteriaDictionary)
{
_criteriaDictionary = criteriaDictionary;
}

public void Store()
{
var criteriaList = new List<Criteria>();
foreach(KeyValuePair<string,string> kvp in _criteriaDictionary)
{
criteriaList.Add(new Criteria(kvp.Key,kvp.Value));
}
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, criteriaList);
}
}
catch (IOException)
{
Debug.Print("error storing criteria");
}

}

public SortedDictionary<string, string> Retrieve( )
{
SortedDictionary<string, string> rtnDict = new
SortedDictionary<string, string>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();

var criteriaList = (List<Criteria>)bin.Deserialize(stream);
foreach (Criteria criteria in criteriaList)
{
rtnDict.Add(criteria.Key, criteria.Value);
}
}
}
catch (IOException)
{
Debug.Print("error retrieving criteria");
}
return rtnDict;

//instead of returning from method, can i fill the byref input
dictionary?
//_criteriaDictionary = rtnDict;
}

}
 
A

Arne Vajhøj

does this seem a reasonable way to serialize a sorted dictionary?
it works which is cool, but are there issues?
calling code:
sdictCriteriaList_Selected is a SortedDictionary<string,string>
private void btnStoreCriteria_Click(object sender, EventArgs e)
{
CriteriaStore cs = new
CriteriaStore(sdictCriteriaList_Selected);
cs.Store();
}

private void btnRetrieveCriteria_Click(object sender, EventArgs e)
{
SortedDictionary<string, string> sdReadBack;
CriteriaStore cs = new CriteriaStore();
sdReadBack = cs.Retrieve();
//just to see that it's working...
foreach (KeyValuePair<string, string> kvp in sdReadBack)
{ Debug.Print("key " + kvp.Key + " value " + kvp.Value); }
}

the class
[Serializable()]
public class Criteria
{
public string Key { get; set; }
public string Value { get; set; }

public Criteria(string key, string value)
{
Key = key;
Value=value;}

}
class CriteriaStore
{
private SortedDictionary<string, string> _criteriaDictionary;
public CriteriaStore()//to retrieve don't need input arg
{}

public CriteriaStore(SortedDictionary<string,string>
criteriaDictionary)
{
_criteriaDictionary = criteriaDictionary;
}

public void Store()
{
var criteriaList = new List<Criteria>();
foreach(KeyValuePair<string,string> kvp in _criteriaDictionary)
{
criteriaList.Add(new Criteria(kvp.Key,kvp.Value));
}
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, criteriaList);
}
}
catch (IOException)
{
Debug.Print("error storing criteria");
}

}

public SortedDictionary<string, string> Retrieve( )
{
SortedDictionary<string, string> rtnDict = new
SortedDictionary<string, string>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();

var criteriaList = (List<Criteria>)bin.Deserialize(stream);
foreach (Criteria criteria in criteriaList)
{
rtnDict.Add(criteria.Key, criteria.Value);
}
}
}
catch (IOException)
{
Debug.Print("error retrieving criteria");
}
return rtnDict;

//instead of returning from method, can i fill the byref input
dictionary?
//_criteriaDictionary = rtnDict;
}

}

Both SortedDictionary and String are serializable, so I think the
code can be shortened quite a bit.

Demo:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;

namespace E
{
public static class SortedDictionaryPersister
{
public static void Save<TKey, TValue>(this
SortedDictionary<TKey, TValue> d, string fnm)
{
using(Stream stm = new FileStream(fnm, FileMode.Create,
FileAccess.Write))
{
BinaryFormatter fmt = new BinaryFormatter();
fmt.Serialize(stm, d);
}
}
public static SortedDictionary<TKey, TValue> Load<TKey,
TValue>(string fnm)
{
using(Stream stm = new FileStream(fnm, FileMode.Open,
FileAccess.Read))
{
BinaryFormatter fmt = new BinaryFormatter();
return (SortedDictionary<TKey,
TValue>)fmt.Deserialize(stm);
}
}
}
public class Program
{
public static void Main(string[] args)
{
SortedDictionary<string, string> d1 = new
SortedDictionary<string, string>();
d1.Add("C", "Canada");
d1.Add("B", "Brazil");
d1.Add("A", "Australia");
foreach(KeyValuePair<string, string> kvp in d1)
{
Console.WriteLine(kvp.Key + " = " + kvp.Value);
}
d1.Save(@"C:\sd.bin");
SortedDictionary<string, string> d2 =
SortedDictionaryPersister.Load<string, string>(@"C:\sd.bin");
foreach(KeyValuePair<string, string> kvp in d2)
{
Console.WriteLine(kvp.Key + " = " + kvp.Value);
}
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

does this seem a reasonable way to serialize a sorted dictionary?
it works which is cool, but are there issues?
calling code:
sdictCriteriaList_Selected is a SortedDictionary<string,string>
private void btnStoreCriteria_Click(object sender, EventArgs e)
{
CriteriaStore cs = new
CriteriaStore(sdictCriteriaList_Selected);
cs.Store();
}

private void btnRetrieveCriteria_Click(object sender, EventArgs e)
{
SortedDictionary<string, string> sdReadBack;
CriteriaStore cs = new CriteriaStore();
sdReadBack = cs.Retrieve();
//just to see that it's working...
foreach (KeyValuePair<string, string> kvp in sdReadBack)
{ Debug.Print("key " + kvp.Key + " value " + kvp.Value); }
}

the class
[Serializable()]
public class Criteria
{
public string Key { get; set; }
public string Value { get; set; }

public Criteria(string key, string value)
{
Key = key;
Value=value;}

}
class CriteriaStore
{
private SortedDictionary<string, string> _criteriaDictionary;
public CriteriaStore()//to retrieve don't need input arg
{}

public CriteriaStore(SortedDictionary<string,string>
criteriaDictionary)
{
_criteriaDictionary = criteriaDictionary;
}

public void Store()
{
var criteriaList = new List<Criteria>();
foreach(KeyValuePair<string,string> kvp in _criteriaDictionary)
{
criteriaList.Add(new Criteria(kvp.Key,kvp.Value));
}
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, criteriaList);
}
}
catch (IOException)
{
Debug.Print("error storing criteria");
}

}

public SortedDictionary<string, string> Retrieve( )
{
SortedDictionary<string, string> rtnDict = new
SortedDictionary<string, string>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();

var criteriaList = (List<Criteria>)bin.Deserialize(stream);
foreach (Criteria criteria in criteriaList)
{
rtnDict.Add(criteria.Key, criteria.Value);
}
}
}
catch (IOException)
{
Debug.Print("error retrieving criteria");
}
return rtnDict;

//instead of returning from method, can i fill the byref input
dictionary?
//_criteriaDictionary = rtnDict;
}

}

And for serialization to disk consider a text format
like XML serialization instead of binary serialization.

Someday you will use own classes and someday you will change
those classes and then you will regret the binary format.

Arne
 
M

mp

Arne Vajhøj said:
does this seem a reasonable way to serialize a sorted dictionary?
[]

And for serialization to disk consider a text format
like XML serialization instead of binary serialization.

Someday you will use own classes and someday you will change
those classes and then you will regret the binary format.

Arne

thanks i'll try that
mark'
 
M

mp

Arne Vajhøj said:
does this seem a reasonable way to serialize a sorted dictionary?
[]

Both SortedDictionary and String are serializable, so I think the
code can be shortened quite a bit.

Demo:[]> Arne

very nice, thanks
i'd googled and found a response to an old post of mine
that's what i'd adapted
this is even easier
thanks
mark
 
M

mp

Arne Vajhøj said:
On 18-01-2011 20:03, mp wrote:

And for serialization to disk consider a text format
like XML serialization instead of binary serialization.

Someday you will use own classes and someday you will change
those classes and then you will regret the binary format.

Arne

on using System.Runtime.Serialization.Formatters.
the only option i get it binary.

i'm googling xmlSerialize
http://www.jaltiere.com/index.php/2006/08/15/net-serialization-part-ii/
shows a way but it only works with one object, not a whole list/collection
of
i've looked at sevarl others but there are tons to look through
thanks
mark
 
A

Arne Vajhøj

on using System.Runtime.Serialization.Formatters.
the only option i get it binary.

i'm googling xmlSerialize
http://www.jaltiere.com/index.php/2006/08/15/net-serialization-part-ii/
shows a way but it only works with one object, not a whole list/collection
of
i've looked at sevarl others but there are tons to look through

I conclude from another question that you have found
System.Xml.Serialization and XmlSerializer.

Note that the binary formatter and the xml serializer
are actually a bit different in how they handle certain things,
but I will still recommend XmLSerializer. You can see what
you have and edit it if necessary.

Arne
 
M

mp

Arne Vajhøj said:
I conclude from another question that you have found
System.Xml.Serialization and XmlSerializer.

Note that the binary formatter and the xml serializer
are actually a bit different in how they handle certain things,
but I will still recommend XmLSerializer. You can see what
you have and edit it if necessary.

Arne

here's what i have so far, comments?
public static void StoreCriteriaChoices(List<Criteria> criteriaChoices,
string fileName)
{
FileStream fStream=null ;
XmlSerializer serializer=null ;
try
{
fStream = new FileStream(fileName, FileMode.Create);
try
{
serializer = new XmlSerializer(typeof(List<Criteria>));
serializer.Serialize(fStream, criteriaChoices);
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
XmlSerializer ");
Debug.Print(ex.Message);
throw;
}
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
FileStream <" + fileName + ">");
Debug.Print(ex.Message);
throw;
}
finally
{
fStream.Close();
}
Debug.Print("Criteria choices stored in " + fileName );
}

thanks
mark
 
A

Arne Vajhøj

here's what i have so far, comments?
public static void StoreCriteriaChoices(List<Criteria> criteriaChoices,
string fileName)
{
FileStream fStream=null ;
XmlSerializer serializer=null ;
try
{
fStream = new FileStream(fileName, FileMode.Create);
try
{
serializer = new XmlSerializer(typeof(List<Criteria>));
serializer.Serialize(fStream, criteriaChoices);
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
XmlSerializer ");
Debug.Print(ex.Message);
throw;
}
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
FileStream<" + fileName +">");
Debug.Print(ex.Message);
throw;
}
finally
{
fStream.Close();
}
Debug.Print("Criteria choices stored in " + fileName );
}

I would use using statement instead of finally to ensure file
being closed.

Arne
 
M

mp

Arne Vajhøj said:
Arne Vajhøj said:
On 19-01-2011 02:45, mp wrote:
On 18-01-2011 20:03, mp wrote:

And for serialization to disk consider a text format
like XML serialization instead of binary serialization.
[]
finally
{
fStream.Close();
}
Debug.Print("Criteria choices stored in " + fileName );
}

I would use using statement instead of finally to ensure file
being closed.

Arne

right on, thanks
mark'
 
M

mp

Arne Vajhøj said:
[]
I would use using statement instead of finally to ensure file
being closed.

Arne

would that look like the following?:
try
{
using (fStream = new FileStream(fileName, FileMode.Create))
{
try
{
serializer = new XmlSerializer(typeof(List<Criteria>));
serializer.Serialize(fStream, criteriaChoices);
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
XmlSerializer ");
Debug.Print(ex.Message);
throw;
}
}
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
FileStream <" + fileName + ">");
Debug.Print(ex.Message);
throw;
}
 
A

Arne Vajhøj

would that look like the following?:
try
{
using (fStream = new FileStream(fileName, FileMode.Create))
{
try
{
serializer = new XmlSerializer(typeof(List<Criteria>));
serializer.Serialize(fStream, criteriaChoices);
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
XmlSerializer ");
Debug.Print(ex.Message);
throw;
}
}
}
catch (Exception ex)
{
Debug.Print("Unable to StoreCriteriaChoices...Problem with
FileStream<" + fileName +">");
Debug.Print(ex.Message);
throw;
}

That looks much better.

I would probably declare fStream in the using as:

using (Stream fStream = new FileStream(fileName, FileMode.Create))

to further simplify.

Arne
 
M

mp

Arne Vajhøj said:
On 19-01-2011 22:07, mp wrote: []>
That looks much better.

I would probably declare fStream in the using as:

using (Stream fStream = new FileStream(fileName, FileMode.Create))

to further simplify.

Arne

thanks
mark
 

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