Customer.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace BusinessEntities
{
[CompactFormatter.Attributes.Serializable()]
public class Customer
{
public Customer()
{
}
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string phone;
public string Phone
{
get { return phone; }
set { phone = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
/// <summary>
/// 实体类(对象比较) 重载Equals
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (!obj.GetType().Equals(typeof(Customer)))
{
return false;
}
else
{
Customer answer = (Customer)obj;
return (answer.ID == ID
&& answer.Name == Name
&& answer.Phone == Phone
&& answer.Address == Address);
}
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using BusinessEntities;
namespace SerializationMobile
{
public partial class Form1 : Form
{
private Customer customer;
private Customer[] customers;
private byte[] xmlBuffer;
private byte[] binBuffer;
public Form1()
{
InitializeComponent();
}
private Customer NewCustomer()
{
Customer customer = new Customer();
customer.ID = new Random().Next();
customer.Name = "Customer" + customer.ID;
customer.Phone = Guid.NewGuid().ToString();
customer.Address = Guid.NewGuid().ToString();
return customer;
}
private Customer[] NewCustomers()
{
ArrayList list = new ArrayList();
for (int i = 1; i < 1000; i++)
{
list.Add(NewCustomer());
}
return list.ToArray(typeof(Customer)) as Customer[];
}
private void Form1_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
customer = NewCustomer();
customers = NewCustomers();
Cursor.Current = Cursors.Default;
}
private void btnXmlSer_Click(object sender, EventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
Cursor.Current = Cursors.WaitCursor;
DateTime startTime = DateTime.Now;
XmlSerializer ser = new XmlSerializer(typeof(Customer));
ser.Serialize(ms, customers);
DateTime endTime = DateTime.Now;
xmlBuffer = ms.ToArray();
//将指定字节数组中的一个字节序列解码为字符串
txtResult.Text = Encoding.ASCII.GetString(xmlBuffer, 0, xmlBuffer.Length);
Cursor.Current = Cursors.Default;
MessageBox.Show(string.Format("字节数:{0}/n耗时:{1}", xmlBuffer.Length, (endTime - startTime)));
}
}
private void btnXmlDeser_Click(object sender, EventArgs e)
{
using (MemoryStream ms = new MemoryStream(xmlBuffer))
{
Cursor.Current = Cursors.WaitCursor;
DateTime startTime = DateTime.Now;
XmlSerializer ser = new XmlSerializer(typeof(Customer));
customers = ser.Deserialize(ms) as Customer[];
DateTime endTime = DateTime.Now;
Cursor.Current = Cursors.Default;
MessageBox.Show(string.Format("耗时:{0}", (endTime-startTime)));
}
}
private void btnBinarySer_Click(object sender, EventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
Cursor.Current = Cursors.WaitCursor;
DateTime startTime = DateTime.Now;
CompactFormatter.CompactFormatter ser = new CompactFormatter.CompactFormatter();
ser.Serialize(ms, customers);
DateTime endTime = DateTime.Now;
binBuffer = ms.ToArray();
txtResult.Text = "";
Cursor.Current = Cursors.Default;
MessageBox.Show(string.Format("字节数:{0}/n耗时:{1}", binBuffer.Length, (endTime - startTime)));
}
}
private void btnBinaryDeser_Click(object sender, EventArgs e)
{
using (MemoryStream ms = new MemoryStream(binBuffer))
{
Cursor.Current = Cursors.WaitCursor;
DateTime startTime = DateTime.Now;
CompactFormatter.CompactFormatter ser = new CompactFormatter.CompactFormatter();
customers = ser.Deserialize(ms) as Customer[];
DateTime endTime = DateTime.Now;
Cursor.Current = Cursors.Default;
MessageBox.Show(string.Format("耗时:{0}", (endTime - startTime)));
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
// 向上导航
// 向上键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
// 向下导航
// 向下键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
// 向左键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
// 向右键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
// Enter
}
}
}
}