Save the output of the Serialize post to a file called sample.xml
Paste the following C# code directly in LINQPad, change language to ‘C# Program’ and run it.
void Main() { var streamReader = new StreamReader(@"c:\sample.xml"); var people = new List<Person>(); var x = new System.Xml.Serialization.XmlSerializer(people.GetType()); var objects = x.Deserialize(streamReader); objects.Dump(); } //Common classes used for the Serialize and Deserialize code examples public class Person { public string Forename { get; set; } public string Surname { get; set; } public List<Hobby> Hobbies; public Person() { Hobbies = new List<Hobby>(); } } public class Hobby { public string Name {get; set; } public Hobby() {} //required for serialisation, even though not used by my test code public Hobby(string name) { this.Name = name; } }
Paste the following C# code directly in LINQPad, change language to ‘C# Program’ and run it.
void Main() { //create test object hierarchy with data var people = new List<Person>() { new Person() { Forename = "Greg", Surname = "Woods", Hobbies = new List<Hobby>() { new Hobby("Sailing"), new Hobby("Skiing") } }, new Person() { Forename = "Alison", Surname = "Woods", Hobbies = new List<Hobby>() { new Hobby("Sleeping") } } }; //do the serializing magic var x = new System.Xml.Serialization.XmlSerializer(people.GetType()); var output = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings() { Indent = true, IndentChars = ("\t"), CloseOutput = true }; XmlWriter writer = XmlWriter.Create(output, xmlWriterSettings); x.Serialize(writer, people); writer.Close(); //display the output in LINQPad output.ToString().Dump(); } //Common classes used for the Serialize and Deserialize code examples public class Person { public string Forename { get; set; } public string Surname { get; set; } public List<Hobby> Hobbies; public Person() { Hobbies = new List<Hobby>(); } } public class Hobby { public string Name {get; set; } public Hobby() {} //required for serialisation to work, even though not used by my test code public Hobby(string name) { this.Name = name; } }
I’ve never been a fan of XML. The first time I came across it was around 2001 working for an ISP. We received press releases from the Press Association (or ‘Ananova’ as it was briefly back then) in XMLformat and had to translate them to HTML snippets. After a little investigation as to what was available at the time, and asking some of the more senior developers, I came to the conclusion that the best way to parse these files was using Perl regular expressions. It was primitive, very effective, and easy enough to rewrite should the format change.
Well, there have been all sorts of attempts at making XML easier to use. There are frameworks galore, and a whole new language – XSLT. The proponents of these methods are generally under the illusion that all devs are like them, and dream in XML format. The truth is, most of us will use the format only when necessary, and really, really do not want to learn XSLT.
In my current side project, a windows app for configuring joystick settings in Flight Simulator, I’ve decided to use XML to save the settings. This is one use of XML I approve of. It is good for hierarchies, and the files can be hand edited by advanced users.
I do not however, want to be messing about directly with the XML in my application. So, I’ve been playing around with derializing* the XML config settings into an object hierarchy – which my application will use, before serializing* back to XML for saving.
As a whole new area to me, I was expecting a steep learning curve. Some well read articles from around 2006 stated that it wasn’t straightforward if you had list of objects (IEnumerables) in your object hierarchy. It turns out, in 2010, using .NET 4.0 (maybe earlier versions too), it works well with no special tweaking.
My next two posts will show examples which can be pasted straight into Linqpad.
*U.S. spelling used, because that’s the language of the .NET framework

