序列化类
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Serialization;namespace ConsoleApplication31{ class Program { static void Main(string[] args) { Student stu = new Student() { Age = 10, Class = "Class One", Name = "Tom", Number = 1 }; XmlSerializer ser = new XmlSerializer(typeof(Student)); ser.Serialize(File.Create("D:\\temp\\x.xml"), stu); } } public class People { [XmlAttribute("NAME")] public string Name { set; get; } [XmlAttribute("AGE")] public int Age { set; get; } } [XmlRoot("Root")] public class Student : People { [XmlElement("CLASS")] public string Class { set; get; } [XmlElement("NUMBER")] public int Number { set; get; } }}
序列化List类
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Serialization;namespace ConsoleApplication31{ class Program { static void Main(string[] args) { Listls = new List (); Student stu1 = new Student() { Age = 10, Class = "Class One", Name = "Tom", Number = 1 }; Student stu2 = new Student() { Age = 20, Class = "Class Two", Name = "Daniel", Number = 2 }; ls.Add(stu1); ls.Add(stu2); XmlSerializer ser = new XmlSerializer(typeof(List )); ser.Serialize(File.Create("D:\\temp\\listx.xml"), ls); } } public class People { [XmlAttribute("NAME")] public string Name { set; get; } [XmlAttribute("AGE")] public int Age { set; get; } } [XmlRoot("Root")] public class Student : People { [XmlElement("CLASS")] public string Class { set; get; } [XmlElement("NUMBER")] public int Number { set; get; } }}
参考链接: