<P>序列化对象</P>
<P> public class People<BR> {<BR> [XmlAttribute("NAME")]<BR> public string Name<BR> { set; get; }<BR> [XmlAttribute("AGE")]<BR> public int Age<BR> { set; get; }<BR> }<BR> [XmlRoot("Root")]<BR> public class Student : People<BR> {<BR> [XmlElement("CLASS")]<BR> public string Class<BR> { set; get; }<BR> [XmlElement("NUMBER")]<BR> public int Number<BR> { set; get; }<BR> }</P>
<P>void Main(string[] args)</P>
<P>{</P>
<P> Student stu = new Student()<BR> {<BR> Age = 10,<BR> Class = "Class One",<BR> Name = "Tom",<BR> Number = 1<BR> };<BR> XmlSerializer ser = new XmlSerializer(typeof(Student));<BR> ser.Serialize(File.Create("C:\\x.xml"), stu);</P>
<P>}</P>
<P>反序列化对象</P>
<P> XmlSerializer ser = new XmlSerializer(typeof(Student));<BR> Student stu = ser.Deserialize(File.OpenRead("C:\\x.xml")) as Student;</P>
<P>对象数组序列化</P>
<P> public class People<BR> {<BR> [XmlAttribute("NAME")]<BR> public string Name<BR> { set; get; }<BR> [XmlAttribute("AGE")]<BR> public int Age<BR> { set; get; }<BR> }<BR> [XmlRoot("Root")]<BR> public class Student : People<BR> {<BR> [XmlElement("CLASS")]<BR> public string Class<BR> { set; get; }<BR> [XmlElement("NUMBER")]<BR> public int Number<BR> { set; get; }<BR> }</P>
<P>void Main(string[] args)</P>
<P>{</P>
<P> List<Student> stuList = new List<Student>();<BR> stuList.Add(new Student() { Age = 10, Number = 1, Name = "Tom", Class = "Class One" });<BR> stuList.Add(new Student() { Age = 11, Number = 2, Name = "Jay", Class = "Class Two" });<BR> stuList.Add(new Student() { Age = 12, Number = 3, Name = "Pet", Class = "Class One" });<BR> stuList.Add(new Student() { Age = 13, Number = 4, Name = "May", Class = "Class Three" });<BR> stuList.Add(new Student() { Age = 14, Number = 5, Name = "Soy", Class = "Class Two" });<BR> XmlSerializer ser = new XmlSerializer(typeof(List<Student>));<BR> ser.Serialize(File.Create("C:\\x.xml"), stuList);</P>
<P>}</P>
<P>对象数组反序列</P>
<P> XmlSerializer ser = new XmlSerializer(typeof(List<Student>));<BR> List<Student> stuList = ser.Deserialize(File.OpenRead("C:\\x.xml")) as List<Student>;<BR> foreach (Student s in stuList)<BR> {<BR> MessageBox.Show(string.Format("{0} : {1} : {2} : {3}",<BR> s.Name, s.Age, s.Class, s.Number));<BR> }</P>
<P>序列化Dirctionary</P>
<P> public struct DirectionList<BR> {<BR> [XmlAttribute("Name")]<BR> public string Name;<BR> [XmlElement("Value")]<BR> public int Value;<BR> }</P>
<P>void Main(string[] args)</P>
<P>{</P>
<P> Dictionary<string, int> list = new Dictionary<string, int>();<BR> list.Add("1", 100);<BR> list.Add("2", 200);<BR> list.Add("3", 300);<BR> list.Add("4", 400);<BR> list.Add("5", 500);<BR> list.Add("6", 600);<BR> list.Add("7", 700);<BR> list.Add("8", 800);<BR> list.Add("9", 900);</P>
<P> List<DirectionList> dirList = new List<DirectionList>();<BR> foreach (var s in list)<BR> {<BR> dirList.Add(new DirectionList() { Name = s.Key, Value = s.Value });<BR> }<BR> XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));<BR> ser.Serialize(File.Create("C:\\x.xml"), dirList);</P>
<P>}</P>
<P>这里还要讲一点,在XmlSerializer中,不支持Dirctionary<>类型的对象,所以在序列化这种最常见类型的时候,只能按照它的格式先创建一个可以别序列化的类型,这里我定义了一个结构体,当然你也可以定义成其他的类。将Dictionary<>中的数据依次放进结构体以后就可以放入流中了。</P>
<P>[XmlAttribute("Name")]意思是将这个字段作为xml的属性,属性名跟在“”中</P>
<P>[XmlElement("Value")]意思是将这个字段做为xml的元素。</P>
<P></P>
<P>反序列化Dirctionary</P>
<P><BR> XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));<BR> List<DirectionList> dirList = ser.Deserialize(<BR> File.OpenRead("C:\\x.xml")) as List<DirectionList>;<BR> foreach (var v in dirList)<BR> {<BR> Console.WriteLine("{0} : {1}", v.Name, v.Value);<BR> }</P>