C# Tutorial 18 Serialization
HTML-код
- Опубликовано: 7 мар 2017
- Get the Code Here : goo.gl/Bt7EMm
Best C# Book : amzn.to/2iMArkU
Support me on Patreon : / derekbanas
This part of my C# tutorial is all about serialization. With serialization you can store the state of an object in a file stream, pass it to a remote network and more. We'll store data using BinaryFormatter, XmlSerializer and so much more.
For best results take notes on the cheat sheet provided above as you watch and leave any questions you have.
Thank you to Patreon supporters like the following for helping me make this video
cottageindustriesbuild/
jaryd remillard : instagram: @distant_admiration
bugreplay.com
@kyleaisho
@thetwistedhat
vjFaLk
jaryd remillard : github.com/YearOfProgramming/...
You are the Bob Ross of Programming. You make it approachable and show people that you can just get in there and start messing around.
Thank you for the awesome compliment :) I love Bob Ross!
Maybe in our code there lives a happy class here, and another one there, just like that. And remembere we don't make mistakes just happy little bugs.
@@bergemel A happy little Animal class
lol!
Brilliant. Thanks for sharing this, this is everything I needed to know about it, even down to the lists! Perfect!
Holy. As a beginner programmer just trying to learn how to save some data this is like french to me. I've been taking notes of what I should learn to understand this and I've filled a full page! Still more learning to do!
These tutorials are incredibly helpful, you`re doing a great job. Greetings from Slovakia.
Thank you very much :) I'm glad you like them
Amazing tutorial. I did not know serializing was a thing, and you explained it in a way that makes it memorable and so easy. Thanks! Subscribed.
Thank you very much :)
you showed your master level of knowing the languages by covering these advanced topics in a 20 minute video.
thanks Derek
Thank you for the compliment :) I'm doing my best
Another great tutorial. Succinct, no padding and speaking at good pace (most of the time I have to watch these at 1.25 speed as the guy speaks too slow). Infact, I just watched another guy's vid on another C# topic. It was over an hour; could easily have been condensed into quarter of an hour and still conveyed relevant info. Nothing more annoying wasting time watching something lengthy, for the guy then to say this is how you DON'T do it. Your presentation, in my estimation, is the correct way - bravo.
Thank you for the nice compliment :) My goal is to cover everything fast and as completely as I can. I'm happy you enjoyed it
Doing a C# course and I'm not learning anything because my teacher goes so fast but, thanks to your videos im finally learning something really appreciate your videos thanks a bunch
Thank you for taking the time to tell me they helped :) I appreciate it
Awesome tutorial as usual, Mario!
Wowza, this is awesome!
thank you so so so so so very much Derek.
Thank you :) You're very welcome
Awesome tutorial. Very helpful for getting started with C# Serialization.
Thank you very much :)
Amazing video, thank you! How I wish all my instructors could teach so simply and so succinctly!
Thank you for the nice compliment :)
I'm an experienced developer and I can say with certainty that your videos are awesome!
Thank you for the compliment :)
Very clear and easy to follow. Just what I was looking for. Thanks.
Thank you :) I'm glad it helped
Thanks you so much Derek
Great video, helped me a lot
Great inspiration. You make it look easy to code.
Thank you :) All you're seeing is practice. I'm not that smart. Anyone can do what I do with a little practice
Mario, thanks for the awesome vids as always
Thank you for the secret code :)
Thank you for the video. Much appreciated!
Thank you for watching :)
I've discovered this channel recently and I'm learning C# through this great tutorial (easy to understand and enjoyable to watch)
I know I'm a little bit late so probably you won't respond, but can you explain why it's necessary the "using(FileStream....)" part of the code? What is his purpose?
how did you learn so much of programming no doubt our world will be automated one day because of your well explained and in depth and quality tutorials. i don't think i can get better tutorials in even udemy for an amount of money
Thank you for the compliment :) I just study like everyone else. I don't have any special techniques
Derek you are amazing! I've seen your site with all of your tutorials. How do you fit all of that knowledge into your brain!!!??? Is your brain connected through hyperspace to a planet-sized hyperbrain? 10**6 thanks for all your hard work. It is a great contribution to humanity!
Thank you for the nice compliment :) It is just a matter of making the time to make another video.
best serialization tutorial ever ! thanks :)
Thank you :)
Well done!
Thank you :)
So very grateful for this vid.
I'm happy it helped :)
I just searched what is Serialization? and find the right resource straight way. Great Lecture. Thanks
I'm happy I could help :)
question: at 10:54 why are you doing TextWriter tw = new StreamWriter instead of StreamWriter sw = new StreamWriter? Are there any advantages/disadvantages to one approach over the other? Thanks in advance.
No difference. It's just storing a more specialized class in a variable that is one of its base classes. This would be akin to doing Animal a = new Cat();. Cat is an animal, so it can be stored in an animal variable.
The one downside/choice related to doing so is that the animal variable is now more basic than the cat it's pointing to. Eg.. the animal variable is pointing to a cat object, but later code can only assume its an animal. It cannot assume it's a cat. This means later code using the variable can't use any specialized cat functions like .Purr(), because Animal wouldn't have that function listed. If you wanted to call .Purr() later, you'd want to store it in a Cat variable, but that would come with other consequences like that variable never being able to point to a Dog. So it's just a choice with different consequences.
Amazing lesson! Sir how do you store the object in a db?
Wow. Thank you so much. Such a nice and easy to understand explanation... could you maybe make a video on MEF?
Thank you :) I'll see what I can do
hey its awesome ,can we do this for serializing json as well? how do we do that?
I liked it ANNNNND it was very descriptive ANNNND it went over multiple types of serialization ANNNNNND I'll look for more vids from you.
That’s funny :) Thank you
Hi and thank you for a very instructive video! I am new to c# but have used languages like c/c++ and swift before. My question is: When creating the constructor for Animal (at 4:20) why do you have to cast the return of GetValue? You are already telling GetValue what type you are working with with "typeof()".Thanks in advance!
Mario
Thanks again for another helpful video! :)
I'm happy you enjoyed it :)
The only thing I would add though is if you ever want to serialize secure data, don't use BinaryFormatter as it is open to serialization attacks.
thanks Derek.
I'm very happy to help :) Thank you for watching
question: "(string)info.GetValue("Name", typeof(string));" why isn't the string casting redundant? does the "(typeof)" not carry over?
the knowledge of derek banas is far beyond anything else on this earth
I think my wife said something like that to me last night :)
So you now have time to take care of her? :O
Hello Sir thank you for all the awesome videos...
Thank you :) I'm very happy that they help
Great video again! Can you make video how to edit, delete and read from xml file via Linq to xml?
Hi there, does this work if I want to save the information of a wpf control like a form with all its button locations and events that are assigned to the buttons?
Info for Visual Studio 2017 Release Candidate (RC) users:
Visual Sutio 2017 is out now. If you have the RC installed check your notification centre (it's the flag at the top)
and click on the "Visual Studio Update".
Thank you for sharing :)
very nice tutorial, like the way you're talking.
Thank you very much :)
I need help with this
"Exception unhandled System.InvalidOperationException: '(project name)+(class) cannot be serialized because it does not have a parameterless constructor.'
The code highlighted is
XmlSerializer serializer = new XmlSerializer(typeof(UserData));
Also Excellent video! Liked and Favorited! :)
How would I serialize objects for use with a database, specifically SQLite?
What if when serializing you want to store objects inside the SerializationInfo? Do the objects you add need to be serializable as well?
Ur amazing, I'm not native in English but u saved me
That topic was easier than I expected. I guess it's possible to encrypt the data too?
Mario
Have you seen this stackoverflow.com/questions/5869922/c-sharp-encrypt-serialized-file-before-writing-to-disk
that's cool
can i serliaze sql data? i thought i saw something a few days ago but my search have no results.
I get error saying that file.xml is being used by another process. I cant figure out why, any ideas?
is it possible to Serialize and Deserialize a Dictionnary because it didn't work for me
Surely Mario! :D
The best game ever!
Thank you for tutorial! Now just have to practice this concept Thank You. :)
I just finished up my Mario clone. I hope you like it :)
After completing this , I will learn how to make Mario clone through the playlist you have made :)
Yes definitely
Hello, great video! But I have a question(it may sound funny but I'm still a new learner). The data saved in the file include non-readable symbols. Is it normal? Thanks in advance! :)
I know this tutorial is a bit old, but Derek, why do i get this error - System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'Serialization_attempt_XML_.BankData'.' (im learning this serialization thing, and i just changed class Animal to BankData, thats it (thats because i want to create bank management system)). Anyway, code is all good, just like yours, everything is same. i did write - using Serialization_attempt_XML_; just because when i write in my program.cs it doesnt find BankData class, even tho its public and in same location as the program.cs.
Hello, 5.39 min you swithched between widows and created new file. How?
how to though serialization Write method Parameters to xml?
Hi Derek
I have the same version of vs that you use
when I write
XmlSerializer serializer = new XmlSerializer(typeof(Animal));
I have this mistake
System.InvalidOperationException: 'lesson18.Animal is inaccessible due to its protection level. Only public types can be processed.'
why????
Hi, Is your Animal class marked as public? Here is my code to check against www.newthinktank.com/2017/03/c-tutorial-18/
You didn't define AnimalID in the serialization methods, but they are seen in the Xml files you showed. Is this so? Thanks.😎
I am running the first example of serialization and the object appears in the console but the file does not appear, can someone explain to me?
How come the AnimalID got stored in xml even doe you didnt add that to the Animal serialize method? :o
how to handle nulls though?
Your videos are concise and to the point.
I have a question on sterilization to and from an xml file of Objects that are complex, for instance multiple levels.
Example:
Animals
Type: Dog
Name: Barko
Age: 7
vaccination History
Vaccin
VaccinName: Covid
Vaccindate: 05/29/2019
Vaccin
VaccinName: Polio
Vaccindate: 05/05/2005
How to serialize such objects with multiple levels in XML ?
What if the animal class contains a field which is a class type?
Object cannot be serialized because it does not have a parameterless constructor? when using xmlserializer
how to Save/Load System?
I have a questing sir. I have an algorithm that is doing some math operations (many ifs and methods) and I want to see not only the result but the result of every step of every iteration. So, I defined a global StringBuilder and used sb.AppendFormat(). method at roughly 15 different locations. Also It checks before append, if(checkbox.log==checked). What I wonder is if this "if operations" expensive? And at the end it writes the stringbuilder to txt file via streamwriter. Is there a better way to do it?
That is something that can be handled by Visual Studios debugger. Check this out msdn.microsoft.com/en-us/library/k0k771bt.aspx
Hi derek!:)
Have You your own programs or ios and Android apps?if you have what is they? Any links?
Hi Helsingin, I only make apps for private companies. That is my niche
this dosen't work
using System.Runtime.Serialization.Formatters.Binary;
got error on Formatters.
using System.Runtime.Serialization;
this i get no error on.
have you added the reference to your project?
Nice
Thank you :)
Is it possible to change variable name in output XML file? For example I want in XML instead of . Can we get custom order in output file? For example instead of ? Thanks!
Sure if you change the names and order for your objects and how you store them
In Java I'll use something like this:
@XmlType(propOrder = { "Height", 'Name",'Weight' }) -> for orders
and
@XmlElement(name = "name_of_the_animal") -> for change output
I found the file stuff a bit arcane--haven't seen "using" used that way before, for instance. Otherwise, good stuff.
mario, I just do this to see if anyone else will catch on :-)
Thank you for doing that :) Now you know how many people actually watch the videos
why dont you just use a sqlite database?
I watch all the videos about C# i can find, but i cannot progress. I´m stunned with all the content, topics, types and resources that i keep confusing and forgeting everything. Your tutorials are great stuff, but i dont know what can i change in me to learn all this without quit.
Study object oriented design and it will make more sense. I have an OOD tutorial using Java. It would be fantastic practice to convert it from Java to C# ruclips.net/video/fJW65Wo7IHI/видео.html
Wow thanks for the fast reply man, I am very grateful for all your work for the community. Of course i will follow your advice!
@@germancedres1114 so hows it going? did you give up?
@@MaybeKian Wow, i really didnt remember this comment!. Yes, i quitted a few times, but i started over and over again and now im working as a softw engineer and im very proud of it :D. Thanks for the comment dude.
I know you are working on ML. may I suggest that you do it in C# sense this is your last and most indepth tutorial so far
I've been using C++ for ML. The hard part is figuring out how to simplify the math involved with ML. I don't want to have to go do a 30 part tutorial on the math involved before I start with ML, but at this point that is where I am. I'd prefer to leave math to the Khan Academy because that is what they do.
+Derek Banas thankbyou for your reply derek. feel free to talk about your work any day. intresting to hear about your path as a developer. im at my first job as a developer
can you make a tutorial about get,set, to string?
I've covered getters, setters and ToString already
can i have the episode number?
Here it is ruclips.net/video/5hFuxo4evK8/видео.html
good
Thank you :)
I have used binary and json serialization, and never had to implement that interface. whats up with that?
This is just one way of doing this like many other things with C#
Ah ok
Can you make Tutorial on D3.js !
I'll see what I can do
Derek's pace was too fast, especially to someone that has zero idea what serialization means.
just watch it twice
i dobt even know how to use the programms cool tutorilas yt
To me this tutorial was lacking a lot of explanation as to why you needed to do the things the way you did it, why it was necessary and why that over other alternative options. It needs more information
and boring
Mario!
plz do a full tutorial of android development.
I've done 2 already. They are on my RUclips channel
Derek Banas is this tutorial is enough to be a legit android devloper.
This video definitely covers all of the basics and then some ruclips.net/video/nBD4xhH5vIE/видео.html&list=PLGLfVvz_LVvSPjWpLPFEfOCbezi6vATIh
hello, my question is that (what is difference between serialization and normal store process). please answer my question
Bro the font could be like half the size and it would be easier to read and follow what you're doing.
Sorry it is kind of busy I agree. I'm trying to find a happy medium between desktop viewers and mobile
I think you can show more than 15 lines and still see it nicely on a mobile (although I will point out most people watching C# tutorials are probably on a laptop or other PC).
Luigi
bowsette
bro why a mac. why..... you broke my heart man
Mario :)
Thank you :)
god created the earth...the heavens and all that....well derek banas created god ...he programmed him in a language we all dont know of and derek banas created it and we normal humans dont get to learn the language derek created the gods we pray to in
You're very funny :) I'm guessing you didn't see me get confused while trying to traverse a maze in a mirror :D
lol u say serialization funny
I say many things funny :)
Watched in 2x to save time and understand it too... Didnt understand anything
Lol, same with 1x. So far File I/O was hardest one but now this, which is even harder...