11

我已经使用 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*/);

这里可能出了什么问题?我该怎么做才能从这个错误中恢复?

谢谢!

4

1 回答 1

2

经过一些实验,我已经能够让它再次工作,但我不明白到底是什么问题。

'ModuleHeader' 类最初只有属性 [Serializable],它的属性有属性 [DataMember]。最近,我将 [DataContract] 添加到课程中(似乎合适),这就是导致问题的原因。在 OnSerialized 事件中,我注意到该实例尚未初始化,因此未创建缺少 Id 的对象。一旦我删除了 [DataContract] 属性,反序列化就会再次开始工作。

我将发布一个新问题...

于 2012-09-10T06:13:53.257 回答