Serialization: Cannot convert type string[][] to string[]

2

I have a class with a jagged array. When I try to serialize it, I get the following exception:

System.InvalidOperationException HResult=0x80131509 Message=Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'string[][]' to 'string[]' error CS0029: Cannot implicitly convert type 'string[]' to 'string[][]'

A simple program to reproduce the problem:

using System.IO;
using System.Xml.Serialization;

namespace JaggedArraySerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Class1));
            var class1 = new Class1();
            using (TextWriter stream = new StreamWriter(@"C:\temp\test.xml"))
            { 
                xmlSerializer.Serialize(stream, class1);
            }
        }
    }
}

And class Class1

using System;
using System.Xml.Serialization;

namespace JaggedArraySerialization
{
    [Serializable]
    public class Class1
    {
        [XmlElement]
        public string[][] MyJaggedArray { get; set; }
    }
}

How can I serialize my jagged array?

c#
arrays
xmlserializer
asked on Stack Overflow Apr 12, 2019 by tobre

1 Answer

2

You can specify the type on the property MyJaggedArray, like this:

    [Serializable]
    public class Class1
    {
        [XmlElement(Type = typeof(string[][]))]
        public string[][] MyJaggedArray { get; set; }
    }
answered on Stack Overflow Apr 12, 2019 by RWRkeSBZ

User contributions licensed under CC BY-SA 3.0