序列化
时间:2010-12-03 来源:D.sky
private void btnDe_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"D:\hhh.txt",FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
computer com = (computer)bf.Deserialize(fs);
MessageBox.Show(com.Color+" "+com.price);
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
SqlCommand cmd = new SqlCommand("select * from employee", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
}
private void btnXML_Click(object sender, EventArgs e)
{
computer com1 = new computer { price = "4000", Color = "blue" };
computer com2 = new computer { price = "3000", Color = "red" };
computer[] co = { com1, com2 };
XmlSerializer xs = new XmlSerializer(typeof(computer[]));
FileStream fs = new FileStream(@"D:\xml.xml", FileMode.CreateNew);
xs.Serialize(fs, co);
fs.Flush();
fs.Close();
}
private void btnDeXML_Click(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(computer[]));
FileStream fs = new FileStream(@"D:\xml.xml", FileMode.Open);
computer[]c=(computer[])xs.Deserialize(fs);
MessageBox.Show(c.Length.ToString());
fs.Flush();
fs.Close();
}