// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Xml.Serialization; namespace E { public class Book { public string Title { get; set; } public string Author { get; set; } } public class Program { public static void Standard() { Book o = new Book(); o.Title = "XML Serialization is fun"; o.Author = "Arne"; XmlSerializer xmlser = new XmlSerializer(typeof(Book)); using(StreamWriter sw = new StreamWriter(@"C:\work\book.xml")) { xmlser.Serialize(sw, o); } Book o2; using(StreamReader sr = new StreamReader(@"C:\work\book.xml")) { o2 = (Book)xmlser.Deserialize(sr); } Console.WriteLine(o2.Title + "/" + o2.Author); } public static void Modifying() { Book o = new Book(); o.Title = "XML Serialization is fun"; o.Author = "Arne"; XmlAttributes attrs = new XmlAttributes(); attrs.XmlRoot = new XmlRootAttribute("GoodBook"); XmlAttributeOverrides over = new XmlAttributeOverrides(); over.Add(typeof(Book), attrs); XmlSerializer xmlser = new XmlSerializer(typeof(Book), over); using(StreamWriter sw = new StreamWriter(@"C:\work\book2.xml")) { xmlser.Serialize(sw, o); } Book o2; using(StreamReader sr = new StreamReader(@"C:\work\book2.xml")) { o2 = (Book)xmlser.Deserialize(sr); } Console.WriteLine(o2.Title + "/" + o2.Author); } public static void Main(string[] args) { Standard(); Modifying(); } } }