5

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.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
RasmusW
  • 3,355
  • 3
  • 28
  • 46
  • You could add a property to your class which gives you a string of your array field. Ignore the array field with XmlIgnore and make the property serializable. – Alioza Jun 12 '15 at 08:15

1 Answers1

1

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 */ }
}
Community
  • 1
  • 1
Niels Filter
  • 4,430
  • 3
  • 28
  • 42