Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-14
2
Medium Priority
?
659 Views
Last Modified: 2013-01-17
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
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38775597
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
 
LVL 3

Accepted Solution

by:
contactnaeem earned 2000 total points
ID: 38776416
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question