Serializing an array of integers using XmlSerializer

前端 未结 2 1900
醉梦人生
醉梦人生 2020-12-07 03:07

I\'m encountering a problem while trying to serialize a multi-dimensioned array of integers via XmlSerializer for an XNA project I\'m working on. I\'m able to s

相关标签:
2条回答
  • 2020-12-07 03:28

    Read the inner-exceptions:

    • There was an error reflecting type 'SomeType'. Cannot serialize member 'SomeType.Data' of type 'System.Int32[,,]', see inner exception for more details.
    • Cannot serialize object of type System.Int32[,,]. Multidimensional arrays are not supported.

    So no: multi-dimensional arrays simply aren't supported. You may have to shim it through as a single-dimension array... you can do this by having a separate property that does the translation:

    [XmlIgnore]
    public int[, ,] Data { get; set; }
    
    [XmlElement("Data"), Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public int[] DataDto
    {
        get { /* flatten from Data */ }
        set { /* expand into Data */ }
    } 
    
    0 讨论(0)
  • 2020-12-07 03:44

    It took me a while to figure out what should go into Marc's get and set braces to flatten and expand multi-dimensional arrays.

    Here is my solution for 2D arrays.

    In my case, I know at compile time that one of the dimensions is 4 so I did not have to store (somehow) the array dimensions.

        [XmlIgnore]
        public int[,] Readings { get; set; }
        [XmlArray("Readings")]
        public int[] ReadingsDto { 
            get { return Flatten(Readings); }
            set { Readings = Expand(value, 4); }
        }
    
        public static T[] Flatten<T>(T[,] arr)
        {
            int rows0 = arr.GetLength(0);
            int rows1 = arr.GetLength(1);
            T[] arrFlattened = new T[rows0 * rows1];
            for (int j = 0; j < rows1; j++)
            {
                for (int i = 0; i < rows0; i++)
                {
                    var test = arr[i, j];
                    arrFlattened[i + j * rows0] = arr[i, j];
                }
            }
            return arrFlattened;
        }
        public static T[,] Expand<T>(T[] arr, int rows0)
        {
            int length = arr.GetLength(0);
            int rows1 = length / rows0;
            T[,] arrExpanded = new T[rows0, rows1];
            for (int j = 0; j < rows1; j++)
            {
                for (int i = 0; i < rows0; i++)
                {
                    arrExpanded[i, j] = arr[i + j * rows0];
                }
            }
            return arrExpanded;
        }
    
    0 讨论(0)
提交回复
热议问题