我想通過下面的代碼XmlSerializer的序列化不能自定義類型
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();
我有McPosition
類型的問題進行序列化。
對於下面輸入
5 , 1, "R251" , {1,2,3}
我得到以下系列化
<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>
任何想法,爲什麼它不是正確的序列?
編輯:
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;
}
等....
感謝。
「McPosition」是什麼樣的? –
您能向我們展示您對'McPosition'類型的定義嗎? –
@Marc Gravell,看我的編輯 –