首页 > 代码库 > XML序列化

XML序列化

XmlSerializer xmlSer = new XmlSerializer(typeof(Class1));MemoryStream writer = new MemoryStream();Class1 c2 = new Class1();c2.b = "123";c2.c = 333;c2.cc = new Class2[2];c2.cc[0] = new Class2();c2.cc[0].bb = 223;c2.cc[1] = new Class2();c2.cc[1].bb = 555;xmlSer.Serialize(writer, c2);StreamReader sr = new StreamReader(writer);var str = Encoding.UTF8.GetString(writer.ToArray());Console.WriteLine(str);Console.ReadLine();writer.Flush();writer.Close();

 

 

反序列化

XmlSerializer serializer = new XmlSerializer(typeof(Class1));TextReader reader = new StringReader(str);Class1 c1 = (Class1)serializer.Deserialize(reader);

 

XML序列化