0

I try to serialize a DataTable object to XML file by using the built-in WriteXml Method. So my DataTable looks for example like this:

   class Program
{
    static void Main(string[] args)
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("ACol", typeof(int));
        dataTable.Columns.Add("BCol", typeof(object));

        DataRow workRow1 = dataTable.NewRow();

        List<ClassA> listClassA = new List<ClassA>();
        List<ClassB> listClassB = new List<ClassB>();

        // Create some data
        for (int i = 0; i < 20; i++)
        {
            listClassA.Add(new ClassA { Prop1 = i, Prop2 = (i * i).ToString() });
            listClassB.Add(new ClassB { Prop3 = i.ToString(), Prop4 = i / 3 });

        }

        workRow1["ACol"] = 20;
        workRow1["BCol"] = listClassA;

        DataRow workRow2 = dataTable.NewRow();

        workRow2["ACol"] = 20;
        workRow2["BCol"] = listClassB;

        // Add rows to the datatable
        dataTable.Rows.Add(workRow1);
        dataTable.Rows.Add(workRow2);

        // Serialize the Class
        dataTable.TableName = "MyTable";
        dataTable.WriteXml("myTableSerialized.xml");

        // Free ressources
        dataTable.Dispose();

    }
}

With the Classes ClassA and ClassB:

public class ClassA : IXmlSerializable
{
    public int Prop1 { get; set; }

    public string Prop2 { get; set; }

    public XmlSchema GetSchema() => null;

    public void ReadXml(XmlReader reader)
    {
        reader.MoveToContent();
        reader.ReadElementContentAsInt("Prop1", "");
        reader.ReadElementContentAsString("Prop2", "");
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Prop1", Prop1.ToString());
        writer.WriteElementString("Prop1", Prop2);
    }
}

public class ClassB : IXmlSerializable
{
    public string Prop3 { get; set; }
    public double Prop4 { get; set; }

    public XmlSchema GetSchema() => null;

    public void ReadXml(XmlReader reader)
    {
        reader.MoveToContent();
        reader.ReadElementContentAsInt("Prop3", "");
        reader.ReadElementContentAsDouble("Prop4", "");
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Prop3", Prop3);
        writer.WriteElementString("Prop4", Prop4.ToString());
    }
}

that both implement the IXmlSerializable interface.

So, when I run the program, I get the System.InvalidOperationException because the type 'System.Collections.Generic.List`1 doesn't not implement the IXmlSerializable interface.

How to fix this?

PythonNoob
  • 914
  • 1
  • 7
  • 15
  • Don't think you can put a List in a DataColumn like that and expect things to work. I would try this without a DataTable, so just create a class. – LarsTech Dec 09 '19 at 21:41
  • With XML serialization, `IXmlSerializable` types do not serialize successfully when in a polymorphic type hierarchy. See [How to use XmlSerializer to serialize derived instances?](https://stackoverflow.com/q/48427634/3744182) for why. – dbc Dec 09 '19 at 22:40

0 Answers0