C# Tutorial 18 Serialization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 18 in my c-sharp video tutorial in this part of the tutorial we are going to focus on serialization now serialization is going to allow you to store the state of an object in a file stream or pass it to a remote network or save it as an XML file and a whole bunch of other really cool things so we're going to talk about serialization serialize deserialize binary format or XML serialize and a whole bunch of other different things if my tongue doesn't get tied with all these crazy words like always all of the code is available in the description underneath this video along with the transcript of the video and I have a lot to do so let's get into it ok so here we are once again in visual studio and I went ahead and I created an animal object actually I didn't create it I'm using exactly the same thing that I used in a previous part of the tutorial so you can either pause your screen and type all this in or get the code that's in the description you know in the description that's what I said ok so this is all the stuff that we created in our animal object and what we're going to do here is use serialization to sort this information in a file in XML files a whole bunch of stuff now to use serialization you're going to have to come up here and go using system and runtime and serialization is it going to put it in there for me so I don't have to spell that which is always good thing and then we're going to go system once again and run time again serialization come on stall that for me there you go and for matters and binary ok so that's going to be used for stabilization and awhile I just caught the big s word huh ok right so what we're going to do here is if you want to define that you want to serialize at this class what you need to do is come in here and go a serializable exactly like that and there you are another thing you're going to have to do here is implement the serializable choir interface so serializable and that guy is and it's going to give you an error it's going to tell you need to implement some methods so let's just go show potential fixes and say implement interface to save some time and it interface early if went and created it down here and this is going to be the serialization function which is going to store object data in a file and just a little bit about these guys serialization info is going to hold key value pairs for the data in your object and streaming context we're not going to use right now but basically it's used to hold additional information so we can just get rid of this guy right here and let's go and create everything we need now basically what we need to do is assign key value pairs for all the data so we're just going to come in and go info and add value and I'm going to put in name because our animals all have names and weights and Heights and animal IDs okay so that's the information that is inside of all of these guys so I'm going to follow that up with the data that is going to be assigned or the value that's going to be assigned to this key so we can go info and value once again and here we're going to put in the weights and there is that and then we're also I want to just copy this we don't need to put nyd in there you can if you want but let's just keep that like that and keep that like that okay so it's doing is assigning a value that you have for your object inside of this key we're assigning the key to the data right so that's what that guys doing well then after you have the ability to serialize your data what you need to also be able to do is to deserialize it or remove the object data from a file and how we do that is we go public and animal and then you're going to put serialization info and context in there so let's just copy this and throw that inside of there big one right there and then we need to get the values from info and assign them to our properties and to do that you just go name and then you're going to say what type of data this is the name is a string and then you go info and get value and you go and reference the key and then you're going to say the type of data you're going to be working with which is a string okay so pretty simple stuff and let's go copy this and do this for the height and the weight as well like I said you could do it for the animal idea but I'm not going to do it for this so let's go like this and our weights are going to be doubles and weight is the key and this is a double so we'll change that to double as well and then we will also have our height and that's a double also and then we are going to reference sites and then we'll reference double okay so that's going to allow us to save those objects in files as well as retrieve those file or those data for those different objects from files so got that all saved up and now we're going to jump over into the program file and get this stuff working all right so we're going to have to import a couple modules here for writing to a file we are going to need system IO and I covered that last part of the tutorial if you want to be able to serialize our object into binary formats we're going to binary format and I'm also going to do XML format and then I'll show you how to go back and forth so serialization like that and for matters like that and then binary okay so we got that whoops it's something crazy am i open up the command window there alright and also to serialize into XML you're going to need using and system and XML and serialization and there that is alright so we're going to be using all of those guys so now we'll come down inside of our class and specifically into main and very first thing we're going to do is create an animal object and I'm going to call him Bowser and you just go new animal short this is old hat for you guys and there is Bowser and I don't know for height and weight I'm just going to put 45 25 inside of here just do something and then if I want to serialize the object data to a file I'm going to go a stream and all have already covered streams and we'll go file dots open I'm going to say what I want to open and I'm going to call this an AMOLED data and this is going to be a DAT file and then I just have to define how I you know want to work with it and I'm going to say if it doesn't exist then I want you to create it for me and there we are so I got that data file that's going to we're going to store object data in and I want to store this in by America by neri format so I'm going to call binary formatter just call this bf and new binary formatter and that's all you need to do for that guy then I just need to send the object data to my file so a binary format err and serialize and then a money to say stream and Bowser so that is all it's going to take so that that is now going to be saved and to prove that it's been saved I'm going to come in here and delete the data that was stored with our Bowser object that we have and then I'm going to go in and read the object data from the file so I'm going to go stream once again is equal to file open and specifically what I'm going to be pulling data out of is where I stored it which is in the anima animal data file and this is going to use file mode open this time and then I'm going to go binary formatter is equal to new binary formatter and there it is and then let's roll this up here a little bit I'm going to go to get the data I just go bowser is equal to and I know this is going to be an animal object so I'll go animal like the cast at one animal binary formatter and deserialize and that's an impasse in the streamwork okay so that's all I need to do and of course you want to close your stream and if we want to prove that it is still there or we got all that data remember we deleted it right here so to prove that we were able to get it back out of there we're just going to go Bowser and to string save it and don't think I have any errors let's run it put up to do uh-oh what I do oh I see what I did whenever I was creating this well you see right here I went and I opened a stream to this well it gave me an error because I forgot to close it so there you go that's the importance of always closing one so that closes the stream and if I save that now I bet you'll run and let's run it and you can see it didn't work or it did work how's our weighs 45 pounds and is 25 inches tall ok so pretty cool we were able to save all that information so now what I want to do is let's come down here and I want to show you how we can save we're serialized this information using XML which is very useful but before I do that I'm going to change Bowser a little bit I'm going to change his weight just so we can see that we changed you know something here with our output now an XML serializing to write our object data as XML I'm sure you're very well aware of XML so we're going to go XML serialize and I'm just going to call this serializer equal to new XML serializer and then I have to define what type of that I'm working with so I'm going to say type of animal and I'm going to show you how to say is a list of objects as well if that was coming into your mind when you go using and I'm going to use the text writer this time just call that T W equal to new stream writer we're going to be writing data like I said previously to a file as XML and we're specifically going to be writing these objects so where I want to store that information of course you're going to have to have this directory created so you're going to need a dirt ban stricter you're going to need to change it to something you can use c-sharp and anytime you're working with files remember you may need to come in here and start the Visual Studio like this so go right-click and more and run as administrator if you get any type of permission problems so that's just something else to remember and I'm going to store this in Bowser dot XML and then I'll show you how to also convert back and forth whoops don't want to do that because I need to serialize this and to do that you go serialize are like this and serialize and then we're going to go to our text writer and specifically what we're storing which is going to be the Bowser object and then what I'm going to do is go in and just like I did before I am going to nullify Bowser because we have that information saved and we don't need to close the string because that's the reason why we used using in that situation so now what I want to do is deserialize from XML to our object and to do that we're going to go XML serialize once again and I'm going to call this D serializer like that and new XML serialize and once again we have to say type of and whoops type of and this is going to be an animal then I'm going to use a text reader so I'm going to say text reader and let's just call this reader is equal to new stream reader like that and then we're going to pass in the file we want to work with which is going to be this guy up here so let's just paste that right there close that off and then I'm just going to use a basic object so let's just kill this obj and call our D serializer once again and more specific deserialize and we will pass our reader inside of there now what we can do is go and assign that information back to Bowser just by saying that it is not a regular object it is an animal object so we cast that and in this situation we want to make sure that we close our reader so that we'll be able to access this information again and if we want to come in here remember we changed as the what was at the height or the weight the weight of Bowser 250 there just to show that something different happens and we can then go in and go Bowser to string like that save it and run it and you can see right there we went in it and we were able to save that ok so cool stuff and also you can see if we investigate the xml file that Bowser's data is stored inside of there and there is the exact way the xml data looks right so we got all that setup now what we're going to do is we're going to show how to go and save a collection of animals so we're going to create a list once again and it is going to be an animal list that I'm going to call a la animals and giving a new list animal and then we'll define a whole bunch of different animals and girls for that semicolon there so I don't forget it so I'm going to go new animal and let's call this animal Mario and keep him on top upper case letter and we'll throw 60 and 30 inside of there and then we'll create a couple more of these animals and save them in our list so like that and we could also do Luigi and let's have him be 55 and 24 and then finally let's see if you can take a wild guess no not toad peach I are hanging 10 this is going to be 40 and we'll change this to 20 and get rid of that all right so we have our list of animals we're going to save those as XML once again we're now using stream file stream is equal to new file stream and we're going to save it in the same general place that we saved our other XML files let's go throw that they're like that and let's call this animals instead now inside of here we're going to call xml serialize because i like it and there is xml serializable or serializer and let's call this serializer - is equal to new xml serializer and then we just need to define the type of you know data again well this time it's going to be a list all right so that's not that hard list all animals so there we go we have that inside of there and then after that we need to go serializer to and serialize and then we're just going to pass in our file stream and the animals which is the list that we are going to be converting into xml now we can command again and go the animals and nullify that data and then we can come in and we can read data from that XML file well let's jump over and look at the XML just so we can see what it looks like and there you can see there's all of it so there's Mario sixty deficit the dust you can see that it created all that XML all right so now we're going to jump in and pull it back out so we will go XML serialize and I think I need to call this serializer flurry I think that's what I'm up to is equal to new XML serializer and type of and this is going to be a list and this is going to be an animal list throw that there now I'm going to go using the file stream and I call this file stream to equal two and we'll call file and we need to open and read data out of it and we're going to need to get our file till I save the file there it is and we just need to change this to animals instead of Bowser and there's this and then inside of here we'll go the animals is equal to and we need to cast it to a animal list and then we go serializer three and deserialize so there's that and then we'll go FS two which is what we're working with here our file stream the second one and that's it do that and now we can come in and look to make sure that everything is the same so we will go animal a in animal list or the yeah I want to do raw animals that works and then we can go and we can print this out so we'll go a to string and we got that set up and everything looks good let's run it oh there was an error what was the error file stream does not contain a constructor that takes one arguments and where was that error at that was on line 63 so what is line 63 and what I do wrong oh yes I forgot to put in some additional information so let's just come in here and I also need to come in and do file mode and define that I want to create this I also want to be able to come in and write to the file and then I do not want to file share all right have that saved don't see any errors got that fixed let's run it to data and there you can see Mario weighs 60 pounds 30 inches tall 55 pounds 24 inches tall and peaches 40 pounds and 20 inches tall all right so there is a whole bunch of information about serialization with c-sharp hope you guys found that useful and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 103,724
Rating: undefined out of 5
Keywords: C# Serialization
Id: jbwjbbc5PjI
Channel Id: undefined
Length: 19min 19sec (1159 seconds)
Published: Wed Mar 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.