5/14/2012

Advance .NET – Demo Serializable

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization;// Phải references "System.Runtime.Serialization.Formatters.Soap.DLL" nhé
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;


namespace Demo_Serializable_Cho_CongPt
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {        
        private void btnXML_Click(object sender, System.EventArgs e) //Ghi File dang XML
        {
            try
            {
                int id = int.Parse(txtSinhvienID.Text.Trim());
                string name = txtTenSV.Text.Trim();
                string diachi = txtDC.Text.Trim();

                Sinhvien s = new Sinhvien(id, name, diachi);

                SoapFormatter soapF = new SoapFormatter();// Dạng mã hóa dữ liệu XML
                Stream stream = new FileStream(@"C:\Temp\sinhvien_XML.DAT", FileMode.OpenOrCreate, FileAccess.Write);

                soapF.Serialize(stream, s);
                stream.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            MessageBox.Show("Đã ghi dữ liệu dạng XML vào file: \"sinhvien_XML.DAT\"");

            txtSinhvienID.Text = "";
            txtTenSV.Text = "";
            txtDC.Text = "";

        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, System.EventArgs e) //Ghi File dang Ma Hoa
        {
            try
            {
                int id = int.Parse(txtSinhvienID.Text.Trim());
                string name = txtTenSV.Text.Trim();
                string diachi = txtDC.Text.Trim();

                Sinhvien s = new Sinhvien(id, name, diachi);

                BinaryFormatter binary = new BinaryFormatter();//Dạng mã hóa dữ liệu nhị phân
                Stream stream = new FileStream(@"C:\Temp\sinhvien_MaHoa.DAT", FileMode.OpenOrCreate, FileAccess.Write);

                binary.Serialize(stream, s);
                stream.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            MessageBox.Show("Đã ghi dữ liệu dạng mã hóa nhị phân vào file: \"sinhvien_MaHoa.DAT\"");

            txtSinhvienID.Text = "";
            txtTenSV.Text = "";
            txtDC.Text = "";
        }

        private void btnReadXML_Click(object sender, System.EventArgs e)
        {
            SoapFormatter formatter = new SoapFormatter();// Ghi file dạng gì thì dùng dạng ấy để đọc mới được
            Stream stream = new FileStream(@"C:\Temp\sinhvien_XML.DAT", FileMode.Open, FileAccess.Read);

            Sinhvien s = (Sinhvien)formatter.Deserialize(stream); // Dịch ngược file--Tức đọc file
            stream.Close();

            txtSinhvienID.Text = s.ID.ToString();
            txtTenSV.Text = s.Name;
            txtDC.Text = s.Diachi;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            BinaryFormatter formatter = new BinaryFormatter();// Ghi file dạng gì thì dùng dạng ấy để đọc mới được
            Stream stream = new FileStream(@"C:\Temp\sinhvien_MaHoa.DAT", FileMode.Open, FileAccess.Read);

            Sinhvien s = (Sinhvien)formatter.Deserialize(stream); // Dịch ngược file--Tức đọc file
            stream.Close();

            txtSinhvienID.Text = s.ID.ToString();
            txtTenSV.Text = s.Name;
            txtDC.Text = s.Diachi;
        }
    }
}
using System;
using System.Windows.Forms;

namespace Demo_Serializable_Cho_CongPt
{
    [Serializable] //Cái này hiểu là class đựợc gọi là để ghi và đọc file theo 2 cách XML và Nhị phân
    public class Sinhvien
    {
        private int id;
        private string name;
        private string diachi;
        public Sinhvien(int id, string name, string diachi)
        {
            this.id = id;
            this.name = name;
            this.diachi = diachi;
        }

        public int ID
        {
            get
            {
                return id;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Diachi
        {
            get
            {
                return diachi;
            }
            set
            {
                diachi = value;
            }
        }

        public void HienthiThongtin()
        {
            string info = "Thong tin Sinh Vien: \n";
            info += "ID la: "+id+"\n";
            info += "Ten la la: "+name+"\n";
            info += "Dia chi la: "+diachi;

            MessageBox.Show(info);
        }
    }
}

Categories:
If You Enjoyed This Post Please Take 5 Seconds To Share It.

0 comments:

Post a Comment

 
  • Followers