当前位置:Gxlcms > mysql > 序列化与反序列化(2)

序列化与反序列化(2)

时间:2021-07-01 10:21:17 帮助过:13人阅读

自动对象序列化是转换XML和二进制对象的最简单方法。需要特别说明的是,自动对象序列化仅对公有数据类型和公有数据成员有效。 public class Person{ public string FirstName { get; set; } public char MiddleInitial { get; set; } public string LastName

自动对象序列化是转换XML和二进制对象的最简单方法。需要特别说明的是,自动对象序列化仅对公有数据类型和公有数据成员有效。
public class Person
{
    public string FirstName { get; set; }
    public char MiddleInitial { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public double HighschoolGPA { get; set; }
    public Address Address { get; set; }
    //to be XML serialized, the type must have
    //a parameterless constructor
    public Person() { }
    public Person(string firstName, char middleInitial, string lastName,
    DateTime birthDate, double highSchoolGpa, Address address)
    {
        this.FirstName = firstName;
        this.MiddleInitial = middleInitial;
        this.LastName = lastName;
        this.BirthDate = birthDate;
        this.HighschoolGPA = highSchoolGpa;
        this.Address = address;
    }

    public override string ToString()
    {
        return FirstName + " " + MiddleInitial + ". " + LastName + ", DOB:" + BirthDate.ToShortDateString() + ", GPA: " + this.HighschoolGPA + Environment.NewLine + Address.ToString();
    }
}
//sorry, don’t feel like listing out 50 states :)
public enum StateAbbreviation { RI, VA, SC, CA, TX, UT, WA };
public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public StateAbbreviation State { get; set; }
    public string ZipCode { get; set; }
    public Address() { }
    public Address(string addressLine1, string addressLine2,
    string city, StateAbbreviation state, string zipCode)
    {
        this.AddressLine1 = addressLine1;
        this.AddressLine2 = addressLine2;
        this.City = city;
        this.State = state;
        this.ZipCode = zipCode;
    }
    public override string ToString()
    {
        return AddressLine1 + Environment.NewLine + AddressLine2 + Environment.NewLine + City + ", " + State + " " + ZipCode;
    }
}


使用简单的代码进行序列化(然后反序列化)。

Person person = new Person("John", 'Q', "Public",
                           new DateTime(1776, 7, 4), 3.5,
                           new Address("1234 Cherry Lane", null, "Smalltown",
                                       StateAbbreviation.VA, "10000"));
Console.WriteLine("Before serialize:" + Environment.NewLine +
                  person.ToString());
XmlSerializer serializer = new XmlSerializer(typeof (Person));
//for demo purposes, just serialize to a string
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
    //the actual serialization
    serializer.Serialize(sw, person);
    Console.WriteLine(Environment.NewLine + "XML:" + Environment.NewLine +
                      sb.ToString() + Environment.NewLine);
}
using (StringReader sr = new StringReader(sb.ToString()))
{
    //deserialize from text back into binary
    Person newPerson = serializer.Deserialize(sr) as Person;
    Console.WriteLine("After deserialize:" + Environment.NewLine +
                      newPerson.ToString());
}


输出为:

Before serialize:

John Q. Public, DOB:7/4/1776, GPA: 3.5

1234 Cherry Lane

Smalltown, VA 10000

XML:

John

81

Public

1776-07-04T00:00:00

3.5

1234 Cherry Lane

Smalltown

VA

10000

After deserialize:

John Q. Public, DOB:7/4/1776, GPA: 3.5

1234 Cherry Lane

Smalltown, VA 10000

人气教程排行