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/...

Комментарии • 170

  • @Atrix78
    @Atrix78 7 лет назад +87

    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.

    • @derekbanas
      @derekbanas  7 лет назад +17

      Thank you for the awesome compliment :) I love Bob Ross!

    • @bergemel
      @bergemel 7 лет назад +19

      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.

    • @RP-mp4ow
      @RP-mp4ow 5 лет назад +4

      @@bergemel A happy little Animal class

    • @josephmarsh5031
      @josephmarsh5031 5 лет назад +1

      lol!

  • @lmaoroflcopter
    @lmaoroflcopter 3 года назад +2

    Brilliant. Thanks for sharing this, this is everything I needed to know about it, even down to the lists! Perfect!

  • @BlankPostalOfficial
    @BlankPostalOfficial 4 года назад +1

    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!

  • @lukasdolniak66
    @lukasdolniak66 7 лет назад +1

    These tutorials are incredibly helpful, you`re doing a great job. Greetings from Slovakia.

    • @derekbanas
      @derekbanas  7 лет назад

      Thank you very much :) I'm glad you like them

  • @Lexyvil
    @Lexyvil 5 лет назад

    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.

  • @2271masoud
    @2271masoud 7 лет назад +9

    you showed your master level of knowing the languages by covering these advanced topics in a 20 minute video.
    thanks Derek

    • @derekbanas
      @derekbanas  7 лет назад +1

      Thank you for the compliment :) I'm doing my best

  • @Russellye5man1
    @Russellye5man1 4 года назад

    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.

    • @derekbanas
      @derekbanas  4 года назад

      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

  • @keneilsmith8796
    @keneilsmith8796 6 лет назад +2

    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

    • @derekbanas
      @derekbanas  6 лет назад +2

      Thank you for taking the time to tell me they helped :) I appreciate it

  • @nikamir6116
    @nikamir6116 7 лет назад

    Awesome tutorial as usual, Mario!

  • @titaniumdiveknife
    @titaniumdiveknife 7 лет назад +1

    Wowza, this is awesome!
    thank you so so so so so very much Derek.

    • @derekbanas
      @derekbanas  7 лет назад +1

      Thank you :) You're very welcome

  • @RyRyQTPi
    @RyRyQTPi 6 лет назад

    Awesome tutorial. Very helpful for getting started with C# Serialization.

  • @user-qh6ii7qt6m
    @user-qh6ii7qt6m Год назад

    Amazing video, thank you! How I wish all my instructors could teach so simply and so succinctly!

    • @derekbanas
      @derekbanas  Год назад

      Thank you for the nice compliment :)

  • @JoseOrtega-oc9ip
    @JoseOrtega-oc9ip 6 лет назад

    I'm an experienced developer and I can say with certainty that your videos are awesome!

    • @derekbanas
      @derekbanas  6 лет назад

      Thank you for the compliment :)

  • @lindalengerich5601
    @lindalengerich5601 5 лет назад

    Very clear and easy to follow. Just what I was looking for. Thanks.

    • @derekbanas
      @derekbanas  5 лет назад

      Thank you :) I'm glad it helped

  • @stevefrt9495
    @stevefrt9495 6 лет назад

    Thanks you so much Derek

  • @altair91100
    @altair91100 2 года назад

    Great video, helped me a lot

  • @tapstach2872
    @tapstach2872 6 лет назад

    Great inspiration. You make it look easy to code.

    • @derekbanas
      @derekbanas  6 лет назад

      Thank you :) All you're seeing is practice. I'm not that smart. Anyone can do what I do with a little practice

  • @Ben-ym4de
    @Ben-ym4de 7 лет назад

    Mario, thanks for the awesome vids as always

    • @derekbanas
      @derekbanas  7 лет назад

      Thank you for the secret code :)

  • @SunnyApples
    @SunnyApples 7 лет назад

    Thank you for the video. Much appreciated!

  • @federicoruocco8129
    @federicoruocco8129 6 лет назад +1

    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?

  • @neelimgoswami6336
    @neelimgoswami6336 6 лет назад

    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

    • @derekbanas
      @derekbanas  6 лет назад

      Thank you for the compliment :) I just study like everyone else. I don't have any special techniques

  • @briancrane7634
    @briancrane7634 7 лет назад

    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!

    • @derekbanas
      @derekbanas  7 лет назад

      Thank you for the nice compliment :) It is just a matter of making the time to make another video.

  • @evviaccia
    @evviaccia 6 лет назад

    best serialization tutorial ever ! thanks :)

  • @alfeproduction6291
    @alfeproduction6291 7 лет назад +1

    Well done!

  • @lee1davis1
    @lee1davis1 6 лет назад

    So very grateful for this vid.

  • @flyingbirds4235
    @flyingbirds4235 5 лет назад

    I just searched what is Serialization? and find the right resource straight way. Great Lecture. Thanks

  • @TheChodex
    @TheChodex 6 лет назад +7

    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.

    • @noggog5106
      @noggog5106 6 лет назад +8

      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.

  • @VANTYCSolutions
    @VANTYCSolutions 3 года назад

    Amazing lesson! Sir how do you store the object in a db?

  • @crazynelo
    @crazynelo 6 лет назад

    Wow. Thank you so much. Such a nice and easy to understand explanation... could you maybe make a video on MEF?

    • @derekbanas
      @derekbanas  6 лет назад +1

      Thank you :) I'll see what I can do

  • @jujjjghj
    @jujjjghj 5 лет назад +1

    hey its awesome ,can we do this for serializing json as well? how do we do that?

  • @charlliemurphy8381
    @charlliemurphy8381 4 года назад

    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.

    • @derekbanas
      @derekbanas  4 года назад +1

      That’s funny :) Thank you

  • @mrjohnzon
    @mrjohnzon 5 лет назад

    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!

  • @RP-mp4ow
    @RP-mp4ow 5 лет назад

    Mario
    Thanks again for another helpful video! :)

    • @derekbanas
      @derekbanas  5 лет назад +1

      I'm happy you enjoyed it :)

  • @dragonore2009
    @dragonore2009 2 года назад +1

    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.

  • @vinuhosanagar1
    @vinuhosanagar1 7 лет назад

    thanks Derek.

    • @derekbanas
      @derekbanas  7 лет назад

      I'm very happy to help :) Thank you for watching

  • @josephmarsh5031
    @josephmarsh5031 5 лет назад +1

    question: "(string)info.GetValue("Name", typeof(string));" why isn't the string casting redundant? does the "(typeof)" not carry over?

  • @lebouski
    @lebouski 7 лет назад

    the knowledge of derek banas is far beyond anything else on this earth

    • @derekbanas
      @derekbanas  7 лет назад +3

      I think my wife said something like that to me last night :)

    • @scopestacker9787
      @scopestacker9787 7 лет назад

      So you now have time to take care of her? :O

  • @crystalsky7615
    @crystalsky7615 7 лет назад

    Hello Sir thank you for all the awesome videos...

    • @derekbanas
      @derekbanas  7 лет назад +1

      Thank you :) I'm very happy that they help

  • @rostikdyakiv5182
    @rostikdyakiv5182 7 лет назад

    Great video again! Can you make video how to edit, delete and read from xml file via Linq to xml?

  • @Alana9374
    @Alana9374 5 лет назад

    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?

  • @scopestacker9787
    @scopestacker9787 7 лет назад

    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".

  • @arnonuhmer3771
    @arnonuhmer3771 4 года назад

    very nice tutorial, like the way you're talking.

  • @m1abramstank755
    @m1abramstank755 3 года назад

    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));

  • @josephmarsh5031
    @josephmarsh5031 5 лет назад

    Also Excellent video! Liked and Favorited! :)

  • @Dustballable
    @Dustballable 7 лет назад

    How would I serialize objects for use with a database, specifically SQLite?

  • @bishan_8617
    @bishan_8617 3 года назад

    What if when serializing you want to store objects inside the SerializationInfo? Do the objects you add need to be serializable as well?

  • @yasmine776
    @yasmine776 9 месяцев назад

    Ur amazing, I'm not native in English but u saved me

  • @Starname64
    @Starname64 7 лет назад +3

    That topic was easier than I expected. I guess it's possible to encrypt the data too?
    Mario

    • @derekbanas
      @derekbanas  7 лет назад +3

      Have you seen this stackoverflow.com/questions/5869922/c-sharp-encrypt-serialized-file-before-writing-to-disk

    • @neelimgoswami6336
      @neelimgoswami6336 6 лет назад

      that's cool

  • @svens3722
    @svens3722 3 года назад

    can i serliaze sql data? i thought i saw something a few days ago but my search have no results.

  • @Wanted_VXRAIL
    @Wanted_VXRAIL 2 года назад

    I get error saying that file.xml is being used by another process. I cant figure out why, any ideas?

  • @mouadmeziani1983
    @mouadmeziani1983 5 лет назад

    is it possible to Serialize and Deserialize a Dictionnary because it didn't work for me

  • @a.b_shots
    @a.b_shots 7 лет назад

    Surely Mario! :D
    The best game ever!
    Thank you for tutorial! Now just have to practice this concept Thank You. :)

    • @derekbanas
      @derekbanas  7 лет назад +1

      I just finished up my Mario clone. I hope you like it :)

    • @a.b_shots
      @a.b_shots 7 лет назад

      After completing this , I will learn how to make Mario clone through the playlist you have made :)

    • @derekbanas
      @derekbanas  7 лет назад

      Yes definitely

  • @gerke0324
    @gerke0324 Год назад

    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! :)

  • @boost5207
    @boost5207 5 лет назад

    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.

  • @overlordking5062
    @overlordking5062 5 лет назад

    Hello, 5.39 min you swithched between widows and created new file. How?

  • @yongpengzhu7476
    @yongpengzhu7476 5 лет назад

    how to though serialization Write method Parameters to xml?

  • @feravladimirovna1044
    @feravladimirovna1044 7 лет назад +2

    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????

    • @derekbanas
      @derekbanas  7 лет назад +2

      Hi, Is your Animal class marked as public? Here is my code to check against www.newthinktank.com/2017/03/c-tutorial-18/

  • @SXTransmission
    @SXTransmission 6 лет назад

    You didn't define AnimalID in the serialization methods, but they are seen in the Xml files you showed. Is this so? Thanks.😎

  • @harrisfotogiannis4039
    @harrisfotogiannis4039 3 года назад

    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?

  • @Robber7
    @Robber7 7 лет назад

    How come the AnimalID got stored in xml even doe you didnt add that to the Animal serialize method? :o

  • @altair91100
    @altair91100 2 года назад

    how to handle nulls though?

  • @dineshsthanki
    @dineshsthanki 8 месяцев назад

    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 ?

  • @nikosvovos
    @nikosvovos 4 месяца назад

    What if the animal class contains a field which is a class type?

  • @roopeshkumarramesh3634
    @roopeshkumarramesh3634 7 лет назад

    Object cannot be serialized because it does not have a parameterless constructor? when using xmlserializer

  • @michak1249
    @michak1249 3 года назад

    how to Save/Load System?

  • @wexwexexort
    @wexwexexort 7 лет назад

    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?

    • @derekbanas
      @derekbanas  7 лет назад

      That is something that can be handled by Visual Studios debugger. Check this out msdn.microsoft.com/en-us/library/k0k771bt.aspx

  • @helsinginaurinko4479
    @helsinginaurinko4479 7 лет назад

    Hi derek!:)
    Have You your own programs or ios and Android apps?if you have what is they? Any links?

    • @derekbanas
      @derekbanas  7 лет назад

      Hi Helsingin, I only make apps for private companies. That is my niche

  • @nicklasisaksson6819
    @nicklasisaksson6819 7 лет назад +2

    this dosen't work
    using System.Runtime.Serialization.Formatters.Binary;
    got error on Formatters.
    using System.Runtime.Serialization;
    this i get no error on.

    • @noneyabiz5787
      @noneyabiz5787 4 года назад

      have you added the reference to your project?

  • @surajbhansingh2220
    @surajbhansingh2220 5 лет назад

    Nice

  • @radziu27
    @radziu27 7 лет назад

    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!

    • @derekbanas
      @derekbanas  7 лет назад

      Sure if you change the names and order for your objects and how you store them

    • @radziu27
      @radziu27 7 лет назад

      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

  • @bartonpaullevenson3427
    @bartonpaullevenson3427 6 лет назад

    I found the file stuff a bit arcane--haven't seen "using" used that way before, for instance. Otherwise, good stuff.

  • @patwicker1358
    @patwicker1358 7 лет назад +4

    mario, I just do this to see if anyone else will catch on :-)

    • @derekbanas
      @derekbanas  7 лет назад

      Thank you for doing that :) Now you know how many people actually watch the videos

  • @IamAWESOME3980
    @IamAWESOME3980 4 года назад

    why dont you just use a sqlite database?

  • @germancedres1114
    @germancedres1114 7 лет назад

    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.

    • @derekbanas
      @derekbanas  7 лет назад +3

      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

    • @germancedres1114
      @germancedres1114 7 лет назад

      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!

    • @MaybeKian
      @MaybeKian 3 года назад +1

      @@germancedres1114 so hows it going? did you give up?

    • @germancedres1114
      @germancedres1114 3 года назад +1

      @@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.

  • @peterl1699
    @peterl1699 7 лет назад

    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

    • @derekbanas
      @derekbanas  7 лет назад

      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.

    • @peterl1699
      @peterl1699 7 лет назад

      +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

  • @Nemer115
    @Nemer115 7 лет назад

    can you make a tutorial about get,set, to string?

    • @derekbanas
      @derekbanas  7 лет назад +2

      I've covered getters, setters and ToString already

    • @Nemer115
      @Nemer115 7 лет назад +1

      can i have the episode number?

    • @derekbanas
      @derekbanas  7 лет назад +2

      Here it is ruclips.net/video/5hFuxo4evK8/видео.html

  • @anpowersoftpowersoft
    @anpowersoftpowersoft 6 лет назад

    good

  • @dosluke
    @dosluke 7 лет назад

    I have used binary and json serialization, and never had to implement that interface. whats up with that?

    • @derekbanas
      @derekbanas  7 лет назад +1

      This is just one way of doing this like many other things with C#

    • @dosluke
      @dosluke 7 лет назад

      Ah ok

  • @tuananhdo1870
    @tuananhdo1870 7 лет назад

    Can you make Tutorial on D3.js !

  • @onyebuchiboss
    @onyebuchiboss 5 лет назад +5

    Derek's pace was too fast, especially to someone that has zero idea what serialization means.

  • @Rtx9070ti
    @Rtx9070ti 4 года назад

    i dobt even know how to use the programms cool tutorilas yt

  • @easypeasy837
    @easypeasy837 6 лет назад +15

    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

  • @ellena1903
    @ellena1903 6 лет назад

    Mario!

  • @jk-he7cl
    @jk-he7cl 7 лет назад

    plz do a full tutorial of android development.

    • @derekbanas
      @derekbanas  7 лет назад

      I've done 2 already. They are on my RUclips channel

    • @jk-he7cl
      @jk-he7cl 7 лет назад

      Derek Banas is this tutorial is enough to be a legit android devloper.

    • @derekbanas
      @derekbanas  7 лет назад

      This video definitely covers all of the basics and then some ruclips.net/video/nBD4xhH5vIE/видео.html&list=PLGLfVvz_LVvSPjWpLPFEfOCbezi6vATIh

  • @ziyapandey3068
    @ziyapandey3068 6 лет назад

    hello, my question is that (what is difference between serialization and normal store process). please answer my question

  • @youonlymemetwice2365
    @youonlymemetwice2365 7 лет назад

    Bro the font could be like half the size and it would be easier to read and follow what you're doing.

    • @derekbanas
      @derekbanas  7 лет назад +1

      Sorry it is kind of busy I agree. I'm trying to find a happy medium between desktop viewers and mobile

    • @youonlymemetwice2365
      @youonlymemetwice2365 7 лет назад

      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).

  • @garycazzell4547
    @garycazzell4547 5 лет назад

    Luigi

  • @kingramses8361
    @kingramses8361 5 лет назад +6

    bowsette

  • @Zainjerr
    @Zainjerr 4 года назад

    bro why a mac. why..... you broke my heart man

  • @ellena1903
    @ellena1903 7 лет назад

    Mario :)

  • @lebouski
    @lebouski 7 лет назад

    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

    • @derekbanas
      @derekbanas  7 лет назад

      You're very funny :) I'm guessing you didn't see me get confused while trying to traverse a maze in a mirror :D

  • @Saladon89
    @Saladon89 7 лет назад

    lol u say serialization funny

    • @derekbanas
      @derekbanas  7 лет назад

      I say many things funny :)

  • @94D33M
    @94D33M 5 лет назад

    Watched in 2x to save time and understand it too... Didnt understand anything

    • @mudzibaba
      @mudzibaba 5 лет назад

      Lol, same with 1x. So far File I/O was hardest one but now this, which is even harder...