Пример #1
0
        private string xmlFileName = Application.StartupPath + "\\TestPerson.xml"; //(file for testing xml

        //To test, run the example, serialize and close the application

        private void btnSerialize_Click(object sender, EventArgs e)
        {
            Person pers = new Person(txtFirstName.Text, txtLastName.Text);

            string strMessage = string.Format("{0} is saved on disk at {1}.{2}{2}{2}You can now exit the application, restart and deserialize!",
                                              pers.FullName, fileName, Environment.NewLine);

            if (!BinaryFileSerializer.Serialize(pers, fileName))
            {
                strMessage = string.Format("{0} could not be saved on disk!", fileName);
            }
            lblMessage.Text = strMessage;
            timer1.Enabled  = true;
        }
Пример #2
0
        //To test, start the application and click the "Deserialize" button directly.
        private void btnDeserialize_Click(object sender, EventArgs e)
        {
            Person pers = BinaryFileSerializer.Deserialize <Person>(fileName);

            string strMessage = string.Format("File {0}{1} not found.{1}Deserialization did not succeed!", fileName, Environment.NewLine);

            if ((pers != null))
            {
                strMessage = string.Format("{0} is deserialized into an object of {1} successfully! ",
                                           pers.FullName, pers.GetType().ToString());
                txtFirstName.Text = pers.FirstName.ToUpper();
                txtLastName.Text  = pers.LastName.ToUpper();
            }

            lblMessage.Text = strMessage;
            timer1.Enabled  = true;
        }