Serializable of an XPO Object
public class SerializablePerson : XPObject {
public SerializablePerson() { }
public SerializablePerson(Session session) : base(session) { }
public SerializablePerson(string name, int age) {
this.name = name;
this.age = age;
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private int age;
public int Age {
get { return age; }
set { age = value; }
}
private SerializablePerson parent;
[XmlIgnore] // This would loop, and it's not needed
[Association("Person-Children")]
public SerializablePerson Parent {
get { return parent; }
set { parent = value; }
}
[XmlIgnore] // We'll serialize the helper instead
[Association("Person-Children"), Aggregated]
public XPCollection<SerializablePerson> Children {
get { return GetCollection<SerializablePerson>("Children"); }
}
AssociationXmlSerializationHelper childenSerializationHelper;
[XmlArray("Children")]
[XmlArrayItem(typeof(SerializablePerson))] // extend this as necessary
public AssociationXmlSerializationHelper ChildrenSerializationHelper {
get {
if (childenSerializationHelper == null)
childenSerializationHelper = new AssociationXmlSerializationHelper(Children);
return childenSerializationHelper;
}
}
}
[TestFixture]
public class Tests {
[Test]
public void Test( ) {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializablePerson));
TextWriter textWriter = new StringWriter( );
SerializablePerson parent = new SerializablePerson("Willy Webb", 53);
parent.Children.Add(new SerializablePerson("Billy Blab", 14));
xmlSerializer.Serialize(textWriter, parent);
string str = textWriter.ToString( );
Console.WriteLine(str);
TextReader textReader = new StringReader(str);
SerializablePerson loadedParent = (SerializablePerson) xmlSerializer.Deserialize(textReader);
Assert.IsFalse(object.ReferenceEquals(parent, loadedParent));
Assert.AreEqual(parent.Name, loadedParent.Name);
Assert.AreEqual(parent.Age, loadedParent.Age);
Assert.AreEqual(parent.Children.Count, loadedParent.Children.Count);
Assert.AreEqual(parent.Children[0].Name, loadedParent.Children[0].Name);
Assert.AreEqual(parent.Children[0].Age, loadedParent.Children[0].Age);
}
}
public SerializablePerson() { }
public SerializablePerson(Session session) : base(session) { }
public SerializablePerson(string name, int age) {
this.name = name;
this.age = age;
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private int age;
public int Age {
get { return age; }
set { age = value; }
}
private SerializablePerson parent;
[XmlIgnore] // This would loop, and it's not needed
[Association("Person-Children")]
public SerializablePerson Parent {
get { return parent; }
set { parent = value; }
}
[XmlIgnore] // We'll serialize the helper instead
[Association("Person-Children"), Aggregated]
public XPCollection<SerializablePerson> Children {
get { return GetCollection<SerializablePerson>("Children"); }
}
AssociationXmlSerializationHelper childenSerializationHelper;
[XmlArray("Children")]
[XmlArrayItem(typeof(SerializablePerson))] // extend this as necessary
public AssociationXmlSerializationHelper ChildrenSerializationHelper {
get {
if (childenSerializationHelper == null)
childenSerializationHelper = new AssociationXmlSerializationHelper(Children);
return childenSerializationHelper;
}
}
}
[TestFixture]
public class Tests {
[Test]
public void Test( ) {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializablePerson));
TextWriter textWriter = new StringWriter( );
SerializablePerson parent = new SerializablePerson("Willy Webb", 53);
parent.Children.Add(new SerializablePerson("Billy Blab", 14));
xmlSerializer.Serialize(textWriter, parent);
string str = textWriter.ToString( );
Console.WriteLine(str);
TextReader textReader = new StringReader(str);
SerializablePerson loadedParent = (SerializablePerson) xmlSerializer.Deserialize(textReader);
Assert.IsFalse(object.ReferenceEquals(parent, loadedParent));
Assert.AreEqual(parent.Name, loadedParent.Name);
Assert.AreEqual(parent.Age, loadedParent.Age);
Assert.AreEqual(parent.Children.Count, loadedParent.Children.Count);
Assert.AreEqual(parent.Children[0].Name, loadedParent.Children[0].Name);
Assert.AreEqual(parent.Children[0].Age, loadedParent.Children[0].Age);
}
}