.Net下的XML序列化(一)
XML序列化可以让你使用class-friendly的方式操作XML。我们可以方便的将某个类序列化成XML字符串或文件,这里是一个例子。
Address类:
[Serializable]
public class Address
{
public Address(){}
public string Street
{
get { return street; }
set { street = value; }
}private string street;
public string City
{
get { return city; }
set { city = value; }
}private string city;
public string State
{
get { return state; }
set { state = value; }
}private string state;
}
public class Address
{
public Address(){}
public string Street
{
get { return street; }
set { street = value; }
}private string street;
public string City
{
get { return city; }
set { city = value; }
}private string city;
public string State
{
get { return state; }
set { state = value; }
}private string state;
}
Customer类:
[Serializable]
public class Customer
{
public Customer(){}
public string Name
{
get { return name; }
set { name = value; }
}private string name;
public Address Address
{
get { return address; }
set { address = value; }
}private Address address;
}
public class Customer
{
public Customer(){}
public string Name
{
get { return name; }
set { name = value; }
}private string name;
public Address Address
{
get { return address; }
set { address = value; }
}private Address address;
}
必须在将要序列化的类上加入特性[Serializable]
生成测试数据:
public static Customer GetGustomer()
{
Customer customer = new Customer();
Address address = new Address();
address.City = "北京";
address.State = "丰台";
address.Street = "马家堡西里";
customer.Address = address;
customer.Name = "BillChen";
return customer;
}
{
Customer customer = new Customer();
Address address = new Address();
address.City = "北京";
address.State = "丰台";
address.Street = "马家堡西里";
customer.Address = address;
customer.Name = "BillChen";
return customer;
}
进行序列化操作。
[STAThread]
static void Main(string[] args)
{
Customer customer = Customers.GetGustomer();
SerializerCustomer1(customer);
SerializerCustomer2(customer);
SerializerCustomer3(customer);
Console.ReadLine();
}
private static void SerializerCustomer1(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
FileStream stream = new FileStream("test.xml", FileMode.OpenOrCreate);
ser.Serialize( stream, customer );
stream.Close();
}
private static void SerializerCustomer2(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
MemoryStream stream = new MemoryStream(100);
ser.Serialize( stream, customer );
stream.Position = 0;
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
Console.Write(reader.ReadToEnd());
}
}
private static void SerializerCustomer3(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
MemoryStream stream = new MemoryStream(100);
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;//缩进
ser.Serialize( writer, customer );
stream.Position = 0;
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
writer.Close();
}
static void Main(string[] args)
{
Customer customer = Customers.GetGustomer();
SerializerCustomer1(customer);
SerializerCustomer2(customer);
SerializerCustomer3(customer);
Console.ReadLine();
}
private static void SerializerCustomer1(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
FileStream stream = new FileStream("test.xml", FileMode.OpenOrCreate);
ser.Serialize( stream, customer );
stream.Close();
}
private static void SerializerCustomer2(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
MemoryStream stream = new MemoryStream(100);
ser.Serialize( stream, customer );
stream.Position = 0;
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
Console.Write(reader.ReadToEnd());
}
}
private static void SerializerCustomer3(Customer customer)
{
XmlSerializer ser = new XmlSerializer(typeof(Customer));
MemoryStream stream = new MemoryStream(100);
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;//缩进
ser.Serialize( writer, customer );
stream.Position = 0;
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
writer.Close();
}
以上是序列化指定的类,及读取序列化后的XML内容的几种方式。