我已经使用 DataContractSerializer 将一组对象保存到一个文件中。直到最近我开始收到以下消息时,它一直运行良好。
Deserialized object with reference id '15' not found in stream
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.GetExistingObject(String id, Type type, String name, String ns)
bei ReadAbgleichSettingsFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
bei System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
bei ReadUnitOfTestFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
bei System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
bei ReadArrayOfBaseModuleFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
bei System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
bei System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
bei System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
bei System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlReader reader)
bei Pruef.Net.Model.ModuleManager.LoadModules() in D:\\my_workspaces\\visualstudioprojects\\Pruef.Net\\Pruef.Net\\PruefMvvm\\Model\\ModuleManager.cs:Zeile 626.
该问题似乎与 AbgleichSettings 类有关。
[DataContract(Name = "AbgleichSettings")]
public class AbgleichSettings
{
#region PROPERTIES
/// <summary>
/// Enable feature
/// </summary>
[DataMember(Name = "IsEnabled")]
public bool IsEnabled { get; set; }
/// <summary>
/// Display units after value
/// </summary>
[DataMember(Name="Units", IsRequired=false)]
public string Units { get; set; }
/// <summary>
/// Multiply value by factor
/// </summary>
[DataMember(Name = "Factor")]
public double Factor { get; set; }
/// <summary>
/// Show decimal places 0-3
/// </summary>
[DataMember(Name = "DecimalPlaces")]
public int DecimalPlaces { get; set; }
#endregion
[OnDeserializing]
void OnDeserializing(StreamingContext context)
{
IsEnabled = false;
Units = "";
Factor = 1;
DecimalPlaces = 0;
}
public AbgleichSettings()
{
IsEnabled = false;
Units = "";
Factor = 1;
DecimalPlaces = 0;
}
}
请注意,稍后在我尝试解决问题时添加了 DataMember.Name 属性。
我查看了 XML,发现 Id="15" 的对象是一个 {Comment} 元素,它是另一个类中的字符串属性。
<Header>
<ChangeInfo z:Id="14">
<Author i:nil="true" />
<Comment z:Id="15"></Comment>
<Date>0001-01-01T00:00:00</Date>
</ChangeInfo>
</Header>
....
<Abgleich z:Id="34">
<DecimalPlaces>0</DecimalPlaces>
<Factor>1</Factor>
<IsEnabled>false</IsEnabled>
<Units z:Ref="15" i:nil="true" />
</Abgleich>
** 序列化器 **
return new DataContractSerializer(Modules.GetType(), null,
int.MaxValue /*maxItemsInObjectGraph*/,
true /*ignoreExtensionDataObject*/,
true /*preserveObjectReferences : this is where the magic happens */,
null /*dataContractSurrogate*/);
这里可能出了什么问题?我该怎么做才能从这个错误中恢复?
谢谢!