Results 1 to 1 of 1

Thread: Custom Class Serialization

  1. #1

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Custom Class Serialization

    Hey,

    VB.Net Version here.

    I have seen a number of posts lately regarding the serialization and deserialization of custom classes, so I thought I would write a quick example.

    In some cases, the built in My.Settings functionality does not cover every single thing that you might want to achieve, in which case, it is possible to create a custom class to hold all your variables and then serialize them to a file when you application exits. Then, when you application starts again, you can deserialize the information and load the variables back into your application.

    Out of the box, the .Net Framework allows numerous ways to serialize your data. For instance:
    • BinaryFormatter
    • XmlSerializer
    • SoapFormatter


    If you are performing serialization for the sole use within your own application, then the BinaryFormatter will be sufficient, however, if you need to operate across different operating systems, or networks, or conform to a specific XML Schema, then you will have to use one of the other systems, as BinaryFormatter will not work.

    I have attached a sample project which creates an instance of a custom class which holds 6 String Variables (these could be any type of variable, I have just chosen strings for simplicity), then depending on what information is written into the corresponding textboxes, the instance of the custom class is written out to the file system using either the BinaryFormatter or the XMLSerializer (If you need to use the SoapFormatter simply change the definitions).

    Once the data has been serialized, it is then possible to recover the information and populate them back into the textboxes.

    The example I have created performs these methods on the click of a button, but this could equally on the close and load event of the form, or however you prefer to implement it.

    This is how the data is serialized using the BinaryFormatter:

    Code:
            //Serialize the information using the Binary Formatter
            private void SerializeBinaryButton_Click(object sender, EventArgs e)
            {
                //Grab the information from the TextBoxes
                GetCustomSettingsFromTextBoxes();
    
                //Create a file to save the data to
                FileStream fs = new FileStream("SerializeBinary.data", FileMode.Create);
    
                //Create a BinaryFormatter object to perform the serialization
                BinaryFormatter bf = new BinaryFormatter();
    
                //Use the BinaryFormatter object to serialize the data to the file
                bf.Serialize(fs, customSettings);
    
                //Close the file
                fs.Close();
    
                //Reset the Textboxes to blank so that we can prove the deserilization
                ResetControls();
            }
    And then loads in again:

    Code:
            private void DeserializeBinaryButton_Click(object sender, EventArgs e)
            {
                //Check to see if the file exists
                if (File.Exists("SerializeBinary.data"))
                {
                    //Open file to read the data from
                    FileStream fs = new FileStream("SerializeBinary.data", FileMode.Open);
    
                    //Create a BinaryFormatter object to perform the deserialization
                    BinaryFormatter bf = new BinaryFormatter();
    
                    //Use the BinaryFormatter object to deserilize the data from the file
                    customSettings = (CustomSettings)bf.Deserialize(fs);
    
                    //Set the textboxes with the values stored in the customSettings class
                    SetTextBoxesFromCustomSettings();
    
                    //Close the file
                    fs.Close();
                }
            }
    There are a few more restrictions placed on XML Serialization, such as:
    • XML serialization cannot be used to serialize private data or object graphs.
    • To create a class that can be serialized, specify the class and all members as public, and create a parameterless constructor.


    However, as long as you meet these requirements, then there is no reason that you can't use XMLSerialization on your class.

    Hope this helps!!! If there are any questions, feel free to post back.

    Gary
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width