序列化

  
    
private void btnSe_Click( object sender, EventArgs e)
{
computer com1
= new computer{ Color = " black " ,price = " 4000 " };
FileStream fs
= new FileStream( @" D:\hhh.txt " , FileMode.Append);
BinaryFormatter bf
= new BinaryFormatter();
bf.Serialize(fs, com1);
fs.Flush();
fs.Close();
}

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();
}

你可能感兴趣的:(序列化)