Report a Bug. Previous Prev. Next Continue. Home Testing Expand child menu Expand. SAP Expand child menu Expand. Web Expand child menu Expand. Must Learn Expand child menu Expand. Big Data Expand child menu Expand. Live Project Expand child menu Expand. In this code sample we will see how to serialize and deserialize objects using C. Here is how serialization works. Image source: Microsoft Docs. Namespaces involved Following namespaces are involved in serialization process, System.
Serialization System. Binary Example 1. Here we have our own class named ClassToSerialize. This class has two public valiables name and age with some default values. We will write this class to a disk file temp. SerializeTest class has two methods SerializeNow and DeSerializeNow which perform the task of serialization and deserialization respectively. The steps for de-serializing the object are similar. The only change is that you need to call deserialize method of BinaryFormatter object.
In fact, there's no need to worry about anything since instances are serialized automatically. Let's go back to the form designer and find the OnClosing event, where we'll double click and call our Serialize method in the handler code. Note: This method's name may vary depending on the name of the main form. If you didn't modify it, it should all look the same. When we open the file, it should be readable. For me, the file looks like this:. The serializing part is now done, so let's move on to deserialization.
From a coding point of view, it's a bit more complex, so we'll go through the entire process again. We'll start by creating a Deserialize method in the main form's code. Then, we'll need to determine whether the XML file with the data even exists. The File class and its Exists string path bool method will help us with that. In the condition's body, we'll create an XmlSerializer instance which will be of the same type as our users List.
Once that's done, we'll create the StreamReader instance with a path to our file and then simply call the Deserialize method on the XmlSerializer. However, there is a small catch, the Deserialize method returns an object. Meaning that we'll have to cast it before we assign our saved users to our existing ones. The whole method, with all of this in mind, looks is as follows:. We'll call this method in the form's Load event.
Now, let's move to the designer, find the Load event in the form's properties , and create its handler method. We'll call our Deserialize method in it and load the users to our ListBox. The entire method looks like this:.
When you open the application, add data, close it, and open it again. It should include all of the users that you added before. In conclusion, I'd like to mention a few things which you'd probably find out soon or later when serializing objects for your applications. Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.
0コメント