Ejemplo n.º 1
0
        /// <summary>
        /// Sreialize an object of Person as an xml-file-  the file will be at the
        /// Application Directory (...\bin)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        ///
        private void btnXMLSerialize_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!", pers.FullName, fileName, Environment.NewLine);

            if (!XMLSerialization.SerializeToFile <Person>(xmlFileName, pers))
            {
                strMessage = string.Format("{0} could not be saved on disk!", fileName);
            }

            lblMessage.Text = strMessage;
            timer1.Enabled  = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read the xmlFileName (data file) and save the data into an object of a person
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void btnXMLDeserialize_Click(object sender, EventArgs e)
        {
            string strMessage = string.Format("File corrupt or not found. Deserialization cannot continue!");

            Person pers = XMLSerialization.DeserializeFromFile <Person>(xmlFileName);

            if (pers != null)
            {
                strMessage = string.Format("{0},{2} From the xml-file: {2}{1} back as a Person.",
                                           pers.ToString(), xmlFileName, Environment.NewLine);
                txtFirstName.Text = pers.FirstName.ToUpper();
                txtLastName.Text  = pers.LastName.ToUpper();
            }

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