A simple example of XML serialization:
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Select the Save path
Private void button#click (object sender, EventArgs e)
{
SaveFileDialog1.Filter = "XML file | *. xml ";
If (saveFileDialog1.ShowDialog () = DialogResult. OK)
TextBox1.Text = saveFileDialog1.FileName;
}
// Serialize the class to an XML file
Private void button2_Click (object sender, EventArgs e)
{
If (textBox1.Text = "")
Return;
Mydata newdata = new mydata ();
Newdata. myname = "James ";
Newdata. mybirthday = "09-31 ";
Newdata. age = 23;
Newdata. imageByte = Encoding. Default. GetBytes ("999 adfklasdjfl". ToCharArray ());
XmlSerializer es = new XmlSerializer (typeof (mydata ));
TextWriter writer = new StreamWriter (textBox1.Text );
Es. Serialize (writer, newdata );
Writer. Close ();
}
// Deserialize the XML file as a class
Private void button3_Click (object sender, EventArgs e)
{
XmlSerializer sd = new XmlSerializer (typeof (mydata ));
TextReader drr = new StreamReader (textBox1.Text );
Mydata mm;
Mm = (mydata) sd. Deserialize (drr );
MessageBox. Show ("mm. Name =" + mm. myname + "\ r \ n" +
"Mm. bothday =" + mm. mybirthday + "\ r \ n" +
"Mm. age =" + mm. age + "\ r \ n" +
"Mm. imageByte =" + Encoding. Default. GetString (mm. imageByte ));
}
}
// The class to serializer
Public class mydata
{
Public string myname;
Public string mybirthday;
Public int age;
Public byte [] imageByte;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service