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*/);
ここで何がうまくいかなかったのでしょうか?このエラーから回復するにはどうすればよいですか?
ありがとう!