//Person public class Person { string _name; int _age; char _gender; public Person() { } public Person(string name, int age, char gender) { this._name = name; this._age = age; this._gender = gender; } public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } public char Gender { get { return _gender; } set { _gender = value; } } }
static void Main(string[] args) { List<Person> list = new List<Person>() { new Person("zhangsan1",20,'男'), new Person("zhangsan2",21,'男'), new Person("zhangsan3",22,'女') }; using (FileStream file = new FileStream("xml.xml", FileMode.Create, FileAccess.Write)) { //Type是类型 XmlSerializer xsr = new XmlSerializer(typeof (List<Person>)); xsr.Serialize(file,list); } }
注意:文件保留在bin/debug/下