Click here to Skip to main content
13,001,686 members (103,542 online)
Rate this:
Please Sign up or sign in to 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 10-Oct-12 11:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The approach supporting absolutely any object graph (not only tree) and agnostic of the data does exist. This is Data Contract:[^].

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.

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?
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 :-).
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...
Sergey Alexandrovich Kryukov 10-Oct-12 22:09pm
Sorry, I don't have any particular publication in mind. You see, I don't have a mission to convince you. It looks like I am selling you the idea and you are protecting yourself. This is not the case. I provided you with a comprehensive documentation which was successfully used by many developers who wanted to get some results, not excuses.

If you think this is blah-blah (which is quite rude response to someone who already wastes so much time to really help you), you are welcome to proceed on your own and do all that ad-hoc works you were about to do. If you "have much easier to learn stuff by looking at others work", this is your problem. Decent developers are not looking for "easier" learning, but for best results.

Sorry for somewhat negative reply, but this is because I can see some counterproductive attitude. Just think who would be a looser. Not me -- this persistence issue is not a problem for me. If you don't want get help, don't.

Jackie00100 10-Oct-12 22:42pm
It wasn't to be rude, I think you took it in a wrong way, what is meant by it is that i read what the article say etc, and try to understand it and apply it but in the end it all seems like "bla bla bla" (AKA makes no sense) because no matter how hard i try to understand it, it doesn't work/make sense in the end.

So I don't think this is stupid, or you're trying to sell me the idea or anything, and what would the point even be for me asking if i wasn't interested in the help? to be honest i really appreciate sites like this and people like you that actually trying to help people out for free, and well, "wastes time on losers"... I really try to get my head around it but cant seems to figure it out, and then that happens i prefer to look at others solutions to learn how its done, then go for a better result afterwards.

My apologies for the misunderstanding, (English isn't my native language either) but hope I've cleared some things out here.
Sergey Alexandrovich Kryukov 10-Oct-12 22:47pm
Well, no problem, except one: to make a decision, you need to understand what is available to you. And to understand it, it probably needs some effort (even though, as I say, this topic is very easy, from the usage standpoint -- this thing "just works", without much effort for application development). And from this point of view, which way is easier to understand and other "pedagogical" stuff is totally irrelevant. If you want the result, you will read, try out, etc. Not a rocket surgery.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100