C # basic serialization

Source: Internet
Author: User

Haha... I haven't updated my blog for a long time. It's busy because my projects are waiting to go online at the end of the year. Now, let's write something !~ Today, we will briefly talk about serialization of time relationships.

What is serialization? It is the process of converting the state of an object into a retained or transmitted state. Together with it, there is also a deserialization. It is the process of converting the flow into an object, you can use it together to transfer objects .. Net provides 2 serialization. One is to serialize to an XML file, and the other is to serialize to a binary file. We will demonstrate serialization to an XML file today. See the following code:

 public class Student
{
private string name;
private string sex;
private decimal age;

public Student()
{ }

public Student(string name, string sex, decimal age)
{
this.name = name;
this.sex = sex;
this.age = age;
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Sex
{
get { return sex; }
set { sex = value; }
}

public decimal Age
{
get { return age; }
set { age = value; }
}
}

Here we create an entity class named student which contains three attributes: age, gender, and name. There is nothing to say here, as long as you know that there is such a class here, you can, huh, and then we will create a school to install these students

 [Serializable]
public class School
{
private List<Student> student_List = new List<Student>();
public School()
{ }
public School(List<Student> bk)
{
this.student_List = bk;
}

public List<Student> Student_List
{
get { return student_List; }
set { student_List = value; }
}
}

This is a general list of students that can be installed in a school. Then, the students are added to the list. At that time, note that this class has a Serializable feature, it is useful to indicate that the following code can be persisted into the file. This feature is required to serialize an object.

 Private School GetSchool ()
{
Student b1 = new Student ("boss", "male", 20 );
Student b2 = new Student ("", "female", 10 );
School bs = new School ();
Bs. Student_List.Add (b1 );
Bs. Student_List.Add (b2 );
Return bs;
}

This code is used to add students to the school and read the following code.

 protected void bt1_Click(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(School));
string path = Server.MapPath("Serializer") + @"\xmlPath.xml";
using (StreamWriter sw = new StreamWriter(path))
{
xs.Serialize(sw, GetSchool());
}

}

This means that the serialized code puts a button on the page to instantiate an XmlSerializer object and specify the type of a serializable object. Then we create a folder to save the xml file, then, instantiate a StreamWriter object and set the storage path. Use the Serialize method to persists the object to an XML file. The Serialize method has two parameters. The first one is the stream object used to write the document, the second is the object to be serialized. In this way, the school will be persistently stored in the file. How can I read this file?

  protected void bt2_Click(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(School));
string path = Server.MapPath("Serializer") + @"\xmlPath.xml";
FileStream fs = null;
try
{

fs = new FileStream(path, FileMode.Open, FileAccess.Read);
School BS = (School)xs.Deserialize(fs);
if (BS.Student_List.Count > 0)
{
GridView1.DataSource = BS.Student_List;
GridView1.DataBind();
fs.Dispose();
}

}
catch (IOException d)
{
throw (d);
}

}

This is the second button. when clicked, it will also instantiate an object of the XmlSerializer class to obtain the path for saving the xml file. Create a file stream object and set the path for reading the file stream, file Access Method. Use the Deserialize method to Deserialize the stream, convert the data type to school, and bind the foreground gridview. The running result is as follows:

Well, this is the simplest process of serialization and deserialization. It's very simple. Don't make a brick. Oh, if there's something wrong, please let me know.

 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.