Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Custom Serialization - Part 2

Rate me:
Please Sign up or sign in to vote.
3.86/5 (11 votes)
6 Mar 2008CPOL5 min read 77.2K   624   41  
Custom Serialization in .NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

using BusinessEntities;

namespace WinClient
{
    public partial class frmProduct : Form
    {
        public frmProduct()
        {
            InitializeComponent();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            ClearControls();
        }

        private void buttonDeserialize_Click(object sender, EventArgs e)
        {
            StringReader reader = new StringReader(textBoxResult.Text);
            XmlSerializer serializer = new XmlSerializer(typeof(Product));
            Product product = (Product)serializer.Deserialize(reader);
            ShowData(product);
        }

        private void buttonSerialize_Click(object sender, EventArgs e)
        {
            Product product = new Product();
            product.Id = textBoxId.Text;
            product.Name = textBoxName.Text;
            product.Rate = decimal.Parse(textBoxRate.Text);
            product.Quantity = int.Parse(textBoxQty.Text);

            XmlSerializer serializer = new XmlSerializer(typeof(Product));
            StringWriter writer = new StringWriter();
            serializer.Serialize(writer, product);
            textBoxResult.Text = writer.ToString();
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            ClearControls();
        }

        private void ClearControls()
        {
            textBoxId.Text = string.Empty;
            textBoxName.Text = string.Empty;
            textBoxQty.Text = "0";
            textBoxRate.Text = "0";
        }

        private void ShowData(Product product)
        {
            textBoxId.Text = product.Id;
            textBoxName.Text = product.Name;
            textBoxRate.Text = product.Rate.ToString();
            textBoxQty.Text = product.Quantity.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Cognizant Technology Solutions
United States United States
Solution Architect working for Cognizant Technology Solutions.

Comments and Discussions