C#提供三种序列化方式,分别为:
1、是使用BinaryFormatter进行串行化;
2、使用SoapFormatter进行串行化;
3、使用XmlSerializer进行串行化。其中对于BinaryFormatter的方式需要实现ISerializable接口,而XmlSeriializ不需要实现对应的接口,可以直接序列化。在这里面我们主要采用XMlSerialize来实现对应的序列化操作进而实现对应的对象和XMl文件之间的转换关系。
在通过序列化实现对应的转换关系操作的功能时,我首先创建了Department ,teams,Person三个对象,并设置了对应对象之间的关系,建立的三个对象以及他们之间的对应关系模型如下图所示:
对象的三者的代码为:
public class Team
{
public Team()
{
}
public Team(string Name,string Title)
{
this.Name = Name;
this.Title = Title;
}
public string Name;
public string Title;
public List<Person> Persons;
}
public class Person
{
public Person()
{
}
public Person(string Name,int Age,string Hobby,string Station)
{
this.Name = Name;
this.Age = Age;
this.Hobby = Hobby;
this.Station = Station;
}
public string Name;
public int Age;
public string Hobby;
public string Station;
}
</div>
基于以上三者的对应关系,编写对应的调用函数,实现对应的对象和XMl文件的转换,对应的实现转换的代码为:
<