Solved

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

Posted on 2013-01-14
2
637 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
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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now