.NET XmlSerializer and multidimensional arrays

4.1k views Asked by At

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.

1

There are 1 answers

2
Niels Filter On BEST ANSWER

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 */ }
}