Matthew C
Matthew C

Reputation: 716

deserialize XML to List of objects

I have a List that I populate with ToDo objects and save to XML. This works perfectly. I have problems with deserializing XML back to the List of ToDo objects. I know that there is already few simillar questions but none of them helped me, therefore, I'm writing.

ToDo object class:

public class ToDo
{
    public string ToDoString;
    public DateTime Date;
}

Main program class and methods to serialize and deserialize:

 static class Program
{
    static List<ToDo> ToDoList = new List<ToDo>();

    public static void Serialize(string toDoString)
    {
        // Create and Initialise the object
        ToDo newToDo = new ToDo();
        newToDo.ToDoString = toDoString;
        newToDo.Date = DateTime.Now;

        // Add the object to the List
        ToDoList.Add(newToDo);

        // Serialize List and save to XML
        XmlSerializer mySerializer = new XmlSerializer(ToDoList.GetType());
        // To write to a file, create a StreamWriter object.
        StreamWriter myWriter = new StreamWriter("myXML.xml");
        mySerializer.Serialize(myWriter, ToDoList);
        myWriter.Close();
    }

    public static void Deserialize()
    {
        // This code was originally wrote to deserialize single object. Now I need to deserialize List of objects and this code doesn't work.
        XmlSerializer myDeserializer = new XmlSerializer(ToDoList.GetType());
        FileStream myFileStream = new FileStream("myXML.xml", FileMode.Open);
        deserializeToDo = (ToDo) // Problems start here
        myDeserializer.Deserialize(myFileStream);
        myFileStream.Close();
    }
}

So how do I deserialize XML to List of ToDo objects?

Many thanks for help.

Upvotes: 0

Views: 21957

Answers (3)

DaEagle
DaEagle

Reputation: 136

Remove the line of code with the comment // The problem starts here.

Capture the output of myDeserializer.Deserialize(myFileStream) like this:

public static void Deserialize()
{
    XmlSerializer myDeserializer = new XmlSerializer(ToDoList.GetType());
    FileStream myFileStream = new FileStream("myXML.xml", FileMode.Open);
    var listOfTodos = (List<ToDo>)myDeserializer.Deserialize(myFileStream);
    myFileStream.Close();
}

Upvotes: 1

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

The following works for me

var loadedData = (List<ToDo>)myDeserializer.Deserialize(myFileStream);

I just changed this one line in Deserialize

XmlSerializer myDeserializer = new XmlSerializer(typeof(List<ToDo>));
FileStream myFileStream = new FileStream("myXML.xml", FileMode.Open);
var loadedData = (List<ToDo>)myDeserializer.Deserialize(myFileStream);
myFileStream.Close();

One tip for you

public static void Deserialize()
{
    var myDeserializer = new XmlSerializer(typeof(List<ToDo>));
    List<ToDo> ToDoList;
    using (var myFileStream = new FileStream("myXML.xml", FileMode.Open))
    {
        ToDoList = (List<ToDo>)myDeserializer.Deserialize(myFileStream);
    }
}

using block will close a stream for you.

Upvotes: 4

Faris Zacina
Faris Zacina

Reputation: 14274

You should deserialize it with the following statement:

var deserializedToDoList = (List<ToDo>)myDeserializer.Deserialize(myFileStream);

That should do the work.

Upvotes: 1

Related Questions