Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi everyone, so i recently read about XML and saving data from classes and load it back in, but all examples I've seen only focus on single classes with no interfaces or inherited classes, so I'm alittle confused about how i can do this en a easy way.

My program have 1 abstract super class that have multiple child classes and child-child classes, I'll try to make a class diagram here below:

Superclass --> FirstChild --> SecondChild
Superclass --> ThirdChild
SomeRandomConstantClass (to hold some Items needed by FirstChild)

Everyone of them is putted in a list of super classes (to be able to have everything in one place) but i now wanna know how i i simply can save every data to a single XML and then load it back into the list/classes when clicking a load button?

As I said I've seen it on a single class how to do it with single items, but not with multiple items and inheritance so it would be awesome if some people could provide some useful sample code or articles on how to.

Thanks - Jackie
Posted

1 solution

The approach supporting absolutely any object graph (not only tree) and agnostic of the data does exist. This is Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

Internally, it is based on System.Reflection.Emit. If you need some explanation of how it works, I can explain.

—SA
 
Share this answer
 
Comments
Jackie00100 10-Oct-12 17:49pm    
Don't really get it, isn't XML suppose to be able to save a whole dataset/class data down to a XML table? that's what i understood then i read about it?
Sergey Alexandrovich Kryukov 10-Oct-12 18:44pm    
Well, generally, again, any object graph. This is object-oriented approach. You declare data classes and add attributes indicating you want to consider some members as belonging to contract. Create instances linked together in arbitrary ways, reflected in contract members. Then, at any moment you can use the DataContractSerializer to persist it all in any stream. Later on, you can restored this data to the state as you had before storing it. Totally agnostic the data -- code is emitted automatically based on Reflection data when you first time use serialization. Anyone can create similar mechanism using System.Reflection + System.Reflection.Emit (without Emit, it could be slow), but it requires good understanding of IL.

Any further questions?
--SA
Jackie00100 10-Oct-12 19:13pm    
Sounds very complex, maybe i should just save the data to a text file and manually load/save it, probably more work but easy to do...
Sergey Alexandrovich Kryukov 10-Oct-12 20:02pm    
Not for the usage. Just the opposite: you define your data as you always do -- in memory, never care about XML or any other media -- serializer does.
Of course, you should not manually load/save anything, why? Just love extra pointless work? And you can easily make mistakes, as I can see :-).
--SA
Jackie00100 10-Oct-12 20:13pm    
Can you maybe link to some code or whole project using this type of serializer? Might help more to have some useful code to look at and get an idea how everything works rather than say do bla do bli, (normally i have much easier to learn stuff by looking at others work and use either a debugger with step in or touch the code my self)

I've been reading on it more intense but cant get it to work properly for now...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900