Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

777 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