thsieh
thsieh

Reputation: 620

How to serialize a List of custom object types

I have the class like this

public class Record
{
    public Int32 TotalTrail { get; set; }
    public TimeSpan MyTimeSpan { get; set; }
    public DateTime MyDateTime { get; set; }
}

And I have a List to hold objects of it:

List<Record> _records;

Then when I want to serialize the list:

serializer.Serialize(stream, _records);

There is a runtime error on the above line:

Cannot assign object of type System.Collections.Generic.List`1[[SimpleGame.Record, SimpleGame, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to an object of type SimpleGame.Record.

Is it because I can't serialize the list? How can I fix this?

Upvotes: 2

Views: 9836

Answers (5)

Frank Fernandez
Frank Fernandez

Reputation: 11

I had this same problem: What I did was, I went to the class which you made a list for and added a parameter-less constructor. I read somewhere that serialization works with parameter-less constructors. Once I did this. It worked like a charm! hope it worked.. Something Like this

public class InventoryType
{
    public DateTime Date { get; set; }
    public int ID { get; set; }
    public string RoomT { get; set; }
    public int Quantity { get; set; }

    public InventoryType() { }

    public InventoryType(DateTime date, int id, string roomT, int quantity)
    {

        this.Date = date;
        this.ID = id;
        this.RoomT = roomT;
        this.Quantity = quantity;
    }

    public override string ToString()
    {           
        return "Date: " + Date + "\nID: " + ID + "\nRoom Type: " + RoomT + "\nQuantity: " + Quantity;
    }
}

Upvotes: 0

Ivo
Ivo

Reputation: 8372

You have to create the serializer for the type List<Record> instead of Record

Upvotes: 5

Talha
Talha

Reputation: 19282

Make your class serializable ... Check this link. http://msdn.microsoft.com/en-us/library/4abbf6k0.aspx

Upvotes: 0

Phil
Phil

Reputation: 1973

You don't even need to implement serializable if you want to serialize to XML. Below is a quick easy way to write any object to a file:

Dim s As System.Xml.Serialization.XmlSerializer
Using fs As New IO.FileStream(thePath, FileMode.Create, FileAccess.Write, FileShare.Write)
    Using w As System.Xml.XmlTextWriter = New System.Xml.XmlTextWriter(fs, System.Text.Encoding.Default)
        s = New System.Xml.Serialization.XmlSerializer(GetType(T))
        w.Formatting = Xml.Formatting.Indented
        s.Serialize(w, m_objectToSerialize)
    End Using
End Using

Upvotes: 1

Mayank
Mayank

Reputation: 8852

I think you need to specify that your object is Serializable, following is the example.

[Serializable()]
public class Record 
{ 
    public Int32 TotalTrail { get; set; } 
    public TimeSpan MyTimeSpan { get; set; } 
    public DateTime MyDateTime { get; set; } 
}

Another Sol

you can implement ISerializable interface

public class Record : ISerializable
{ 
    public Int32 TotalTrail { get; set; } 
    public TimeSpan MyTimeSpan { get; set; } 
    public DateTime MyDateTime { get; set; } 
}

Upvotes: 0

Related Questions