I am trying to serialize by following code
var data = argsPerCall.ToArray();
var knownTypes = new[] { typeof(int), typeof(int),
typeof(string), typeof(McPosition)};
var serializer = new XmlSerializer(data.GetType(), knownTypes);
// Writing the file requires a TextWriter.
var myStreamWriter = new StreamWriter(filename);
serializer.Serialize(myStreamWriter, data);
myStreamWriter.Close();
I am having an issue with McPosition type.
For following input
5 , 1, "R251" , {1,2,3}
I am getting following serialization
<ArrayOfAnyType>
<anyType xsi:type="xsd:int">5</anyType>
<anyType xsi:type="xsd:int">1</anyType>
<anyType xsi:type="xsd:string">R251</anyType>
<anyType xsi:type="McPosition" />
</ArrayOfAnyType>
Any idea why it wasnt serialized correctly ?
EDIT:
public struct McPosition : IComparable<McPosition> {
private readonly int _station;
private readonly int _slot;
private readonly int _subslot;
public static McPosition Empty = new McPosition(-1, -1, -1);
public McPosition(int station, int slot, int subslot) {
_station = station;
_slot = slot;
_subslot = subslot;
}
etc….
Thanks .
To be serializeable via
XmlSerializer, each property on a type must have a public getter and setter (and not be marked[XmlIgnore]nor have aShouldSerialize*()that returns false, etc). Public fields are also serialized (as long as they aren’treadonly), but exposing fields is even less desirable.XmlSerializernever looks at private members.I’m guessing (edit: now confirmed by the updated question) that
McPositionis an immutable vector, without public setters. That won’t work. Options:IXmlSerializable(not overly nice, to be honest)McPosition