Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET XmlSerializer and multidimensional arrays

I'm implementing a program which acts as a client to some existing software. The protocol is defined using an XML schema, and it includes multidimensional arrays.

The .net XmlSerializer cannot handle those - it's a known issue.

Is there a way to extend XmlSerializer so I can implement handling for this, or is there a full (free or commercial) replacement for XmlSerializer that can handle multidimensional arrays?

SharpSerializer does not seem to create schema defined XML, but uses it's own serialization format.

I guess I can use sgen.exe to generate the serializer code, and then edit that manually afterwards to add the necessary handling, but I would like to avoid that.

like image 363
RasmusW Avatar asked Nov 10 '22 11:11

RasmusW


1 Answers

If you have control to change your schema, you could try use jagged arrays instead of multidimensional arrays.

string[][] instead of string[,]

or use something like List<List<T>>

You can create your own custom class which flattens the multidimensional array.

Alternatively, use XmlIgnore on your class, flatten the multidimensional array into a standard array (as proposed here by Marc Gravell)

[XmlIgnore]
public int[, ,] Data { get; set; }

[XmlElement("Data"), Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public int[] DataDto
{
    get { /* flatten from Data */ }
    set { /* expand into Data */ }
}
like image 59
Niels Filter Avatar answered Nov 15 '22 07:11

Niels Filter