static void Main(string[] args) { ///////////////////////////////////////////////////////////////// // Serialize the object to an XML file. // // Create and initialize a MySerializableType instance. MySerializableType instance = new MySerializableType(); instance.BoolValue = true; instance.IntValue = 1; instance.StringValue = "Test String"; instance.ListValue.Add("List Item 1"); instance.ListValue.Add("List Item 2"); instance.ListValue.Add("List Item 3"); instance.AnotherTypeValue = new AnotherType(); instance.AnotherTypeValue.IntValue = 2; instance.AnotherTypeValue.StringValue = "Inner Test String"; // Create the serializer XmlSerializer serializer = new XmlSerializer(typeof(MySerializableType)); // Serialize the object to an XML file using (StreamWriter streamWriter = File.CreateText( "CSXmlSerialization.xml")) { serializer.Serialize(streamWriter, instance); } ///////////////////////////////////////////////////////////////// // Deserialize from a XML file to an object instance. // // Deserialize the object MySerializableType deserializedInstance; using (StreamReader streamReader = File.OpenText( "CSXmlSerialization.xml")) { deserializedInstance = serializer.Deserialize(streamReader) as MySerializableType; } // Dump the object Console.WriteLine("BoolValue: {0}", deserializedInstance.BoolValue); Console.WriteLine("IntValue: {0}", deserializedInstance.IntValue); Console.WriteLine("StringValue: {0}", deserializedInstance.StringValue); Console.WriteLine("AnotherTypeValue.IntValue: {0}", deserializedInstance.AnotherTypeValue.IntValue); Console.WriteLine("AnotherTypeValue.StringValue: {0}", deserializedInstance.AnotherTypeValue.StringValue); Console.WriteLine("ListValue: "); foreach (object obj in deserializedInstance.ListValue) { Console.WriteLine(obj.ToString()); } }
static void Main(string[] args) { ///////////////////////////////////////////////////////////////// // 将对象序列化到XML文件 // // 创建并初始化一个MySerializableType实例 MySerializableType instance = new MySerializableType(); instance.BoolValue = true; instance.IntValue = 1; instance.StringValue = "测试 String"; instance.ListValue.Add("List 项 1"); instance.ListValue.Add("List 项 2"); instance.ListValue.Add("List 项 3"); instance.AnotherTypeValue = new AnotherType(); instance.AnotherTypeValue.IntValue = 2; instance.AnotherTypeValue.StringValue = "测试内部元素 String"; // 创建序列化 XmlSerializer serializer = new XmlSerializer(typeof(MySerializableType)); // 将对象序列化到XML文件 using (StreamWriter streamWriter = File.CreateText( "CSXmlSerialization.xml")) { serializer.Serialize(streamWriter, instance); } ///////////////////////////////////////////////////////////////// // 将XML文件反序列化到一个对象的实例 // // 反序列化对象 MySerializableType deserializedInstance; using (StreamReader streamReader = File.OpenText( "CSXmlSerialization.xml")) { deserializedInstance = serializer.Deserialize(streamReader) as MySerializableType; } // 对象结果输出 Console.WriteLine("Bool值: {0}", deserializedInstance.BoolValue); Console.WriteLine("Int值: {0}", deserializedInstance.IntValue); Console.WriteLine("String值: {0}", deserializedInstance.StringValue); Console.WriteLine("自定义类型元素的Int值.IntValue: {0}", deserializedInstance.AnotherTypeValue.IntValue); Console.WriteLine("自定义类型元素的String值: {0}", deserializedInstance.AnotherTypeValue.StringValue); Console.WriteLine("List值: "); foreach (object obj in deserializedInstance.ListValue) { Console.WriteLine(obj.ToString()); } }