Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 664
  • Last Modified:

How do I persist/serialize a Dictionary<String,ObjectClass> to a file in .Net C#?

Hi:

I'm in the middle of designing/implementing saving my C# session to a file.

I'm looking for some help in direction of how to serialize/deserialize several
collections to a file. The file will contain a List<>, several Dictionary<> collections
and a blob of binary data.

I'm thinking that using XML would probably be easier?

What is the best way to proceed?
Are there some good/relevant samples I can look at?

This is what some of my collections look like:
====================================

private Dictionary<String, List<DModel.Entity_Fields>> dEntityFields;

private Dictionary<String, List<ComponentClass>> dComponents;

private Dictionary<String, int> dFieldLengths;

private binary blob of data;

Thanks,
JohnB
0
jxbma
Asked:
jxbma
1 Solution
 
TommySzalapskiCommented:
For the lists, just write the size of the list first, and then all the items. Same with the dictionary entries. If you know there won't be any '\n' characters in a string then that would be a good way to separate strings.
The binary data is easy. Just open the file in binary write mode and write it. Your entity fields may be good to write out in binary as well.
0
 
contactnaeemCommented:
Think through your structure, then design a schema for type of your structure , for each class / collection write save and load methods to populate objects from xml using xml reader and writer. I think that will be efficient and clean way. i.e In your structure you can pass an xml to your top most class and it will pass the reader in hierarchy to the bottom so objects will get populated, simlarly while saving you will call save method of your top most class which will save whole structure. Create Interfaces and abstract classes for common logic. This will be alot more extenedable structure for future enhancements.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now