Rumah > pangkalan data > tutorial mysql > 序列化与反序列化(2)

序列化与反序列化(2)

WBOY
Lepaskan: 2016-06-07 15:44:04
asal
1304 orang telah melayarinya

自动对象序列化是转换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;
    }
}
Salin selepas log masuk


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

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());
}
Salin selepas log masuk


输出为:

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

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan