问题描述
我试图用序列化的XMLSerialization对象时收到以下异常。
I am receiving the following exception when trying to serialize an object using XMLSerialization.
在序列化类型MyObject来的对象}中检测到循环引用
我知道循环引用,因为对象A可以有一个 childObject
对象B的对象B的 parentObject
为对象A和,然而,我想如果可能的话,以保持该引用。有没有办法让这个对象,XML序列化序列化,而不序列化过程中丢失任何数据?我不是很familar与系列化所以我希望孤单的某种属性我可以设置的。
I know the circular reference is because ObjectA can have a childObject
of ObjectB and ObjectB's parentObject
is ObjectA, however I would like to keep that reference if possible . Is there a way to get this object to serialize with XML Serialization without losing any data during the serialization process? I'm not very familar with serialization so I'm hoping theres some kind of Attribute I could set.
推荐答案
有根据串行类型的选项。
There are several options depending on serializer type.
如果你可以使用DataContractSerializer或BinaryFormatter那么您可以使用OnSerializedAttribute并为您的孩子家长对象的属性设置为这样的:
If you could use DataContractSerializer or BinaryFormatter then you may use OnSerializedAttribute and set Parent property for your child object to this:
[Serializable]
public class Child
{
public string Foo { get; set; }
public Parent Parent { get { return parent; } set { parent = value; } }
// We don't want to serialize this property explicitly.
// But we could set it during parent deserialization
[NonSerialized]
private Parent parent;
}
[Serializable]
public class Parent
{
// BinaryFormatter or DataContractSerializer whould call this method
// during deserialization
[OnDeserialized()]
internal void OnSerializedMethod(StreamingContext context)
{
// Setting this as parent property for Child object
Child.Parent = this;
}
public string Boo { get; set; }
public Child Child { get; set; }
}
class Program
{
static void Main(string[] args)
{
Child c = new Child { Foo = "Foo" };
Parent p = new Parent { Boo = "Boo", Child = c };
using (var stream1 = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof (Parent));
serializer.WriteObject(stream1, p);
stream1.Position = 0;
var p2 = (Parent)serializer.ReadObject(stream1);
Console.WriteLine(object.ReferenceEquals(p, p2)); //return false
Console.WriteLine(p2.Boo); //Prints "Boo"
//Prints: Is Parent not null: True
Console.WriteLine("Is Parent not null: {0}", p2.Child.Parent != null);
}
}
}
如果你想使用XmlSerializer您应该实现IXmlSerializable,使用XmlIgnoreAttribute和在ReadXml方法实现或多或少相同的逻辑。但在这种情况下,你也应该手动实现所有的XML序列化的逻辑:
If you want to use XmlSerializer you should implement IXmlSerializable, use XmlIgnoreAttribute and implemented more or less the same logic in ReadXml method. But in this case you should also implement all Xml serialization logic manually:
[Serializable]
public class Child
{
public Child()
{
}
public string Foo { get; set; }
[XmlIgnore]
public Parent Parent { get; set; }
}
[Serializable]
public class Parent
{
public Parent()
{
}
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
//Reading Parent content
//Reading Child
Child.Parent = this;
}
public void WriteXml(System.Xml.XmlWriter writer)
{
//Writing Parent and Child content
}
#endregion
public string Boo { get; set; }
public Child Child { get; set; }
}
这篇关于使用XML序列化时,循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!