How to serialize an XPO object to XML

Description

Can XPO serialize objects to / from an XML file?

Solution

XPO 1.x does not include any means for serialization. You can serialize XPO objects using the same approach as you serialize any other objects:

[C#]


    XmlAttributes attrs = new XmlAttributes();
    attrs.XmlIgnore = true;
    XmlAttributeOverrides ovr = new XmlAttributeOverrides();
    ovr.Add(typeof(XPCustomObject), "GCRecord", attrs);
    XmlSerializer s = new XmlSerializer(typeof(YourObject), ovr);

It's much easier to implement conversion of persistent objects to XML in XPO 6.x. Here is some sample code for XPO 6.1:

[C#]


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);
  }
}
posted @ 2007-04-03 11:46  大洋  阅读(754)  评论(0编辑  收藏  举报