Bongo
Bongo

Reputation: 3153

Serializing Generic Class in C#

i have a problem when trying to serialize an object in C# to XML. I figured out it goes wrong when exporting this class:

[XmlInclude(typeof(CountryData))]
[XmlInclude(typeof(ManufacturerData))]
[XmlInclude(typeof(ProgramData))]

[Serializable()]
public class DeliveryTimeList<T> where T : IDeliveryTimeData
{
    private DataTable _data;
    public DataTable Data
    {
        get { return _data; }
        set { _data = value; }
    }

    public DeliveryTimeList() 
    {
        _data = new DataTable();         
    }

    public void Add(T data) 
    {
        //       Not in Use
        //_data.Rows.Add(data.ItemArray());
    }
}

The class is generic because the next step is to alter the DataTable at the creation of the class and it will contain a List. The classes CountryData, Manufacturer and ProgramData implement the interface IDeliveryTimeData.

I get a System.InvalidOperationException with the following description: Beim Generieren des XML-Dokuments ist ein Fehler aufgetreten (<- German) which roughly translater There was an Error during generation of the XML Document. I don't know why it isn't serializing but i expect that the problem has something to do with that the class is generic.

Has anybody a clue what i am doing wrong ?

Upvotes: 2

Views: 219

Answers (1)

Kev
Kev

Reputation: 286

You cant directly serialize a datatable, you need to serialize a dataset that it is attached to instead

Upvotes: 1

Related Questions