序列化与反序列化(2)

自动对象序列化是转换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:

<?xml version=”1.0” encoding=”utf-16”?>

<Person xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>

    <FirstName>John</FirstName>

    <MiddleInitial>81</MiddleInitial>

    <LastName>Public</LastName>

    <BirthDate>1776-07-04T00:00:00</BirthDate>

    <HighschoolGPA>3.5</HighschoolGPA>

    <Address>

        <AddressLine1>1234 Cherry Lane</AddressLine1>

        <City>Smalltown</City>

        <State>VA</State>

        <ZipCode>10000</ZipCode>

    </Address>

</Person>

After deserialize:

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

1234 Cherry Lane

Smalltown, VA 10000

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值