我使用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>
** Serializer **
return new DataContractSerializer(Modules.GetType(), null,
int.MaxValue /*maxItemsInObjectGraph*/,
true /*ignoreExtensionDataObject*/,
true /*preserveObjectReferences : this is where the magic happens */,
null /*dataContractSurrogate*/);
这里可能出了什么问题?如何从此错误中恢复?
谢谢!
答案 0 :(得分:2)
经过一些实验,我能够让它再次运作,但我不明白究竟是什么问题。
类'ModuleHeader'最初只有[Serializable]属性,其属性具有[DataMember]属性。最近,我在课堂上添加了[DataContract](似乎合适),这就是造成这个问题的原因。在OnSerialized事件中,我注意到实例尚未初始化,因此没有创建缺少Id的对象。删除[DataContract]属性后,反序列化再次开始工作。
我将发布一个新问题......