public static string SerializeToXML(Person p) { XmlSerializer ser = new XmlSerializer(typeof(Person)); string xml; using (StringWriter sw = new StringWriter()) { ser.Serialize(sw, p); xml = sw.ToString(); } return xml; }
static void XMLSerializationDemo() { Person p = new Person { FirstName = "Boris", LastName = "Jeltsin", Age = 87}; string xml = ImTheSerializerGeek.SerializeToXML(p); Console.WriteLine("Serializing Person object...\nOutput:"); Console.WriteLine(xml); Console.WriteLine("Now lets deserialize it! - Press any key to continue..."); Console.ReadLine(); Person pBack = ImTheSerializerGeek.DeserializeFromXML(xml); Console.WriteLine("Result: FirstName:{0}, LastName:{1}, Age:{2}",pBack.FirstName,pBack.LastName,pBack.Age); Console.ReadLine(); }