Deserializing XML to Objects

Created   

Tags: ,

Categories:

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() {
    //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;
    }
}

Comments