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