Xml Import-Export In .NET

This topic applies to .NET version only

One of the most widely used platform independent formats of data exchange today is xml.

Db4o does not provide any specific API to be used for XML import/export, but with the variety of XML serialization tools available for Java and .NET (freeware and licensed) this is not really necessary.

All that you need to export your database/query results is:

  1. Retrieve objects from the database.
  2. Serialize them in XML format (using language, or external tools, or your own serializing software).
  3. Save XML stream (to a disc location, into memory, into another database).

Import process is just the reverse:

  1. Read XML stream
  2. Create an objects from XML
  3. Save objects to db4o

Let's go through a simple example. We will use .NET XmlSerializer. (You can use any other XML serialization tool, which is able to serialize/deserialize classes).

First, let's prepare a database:

SerializeExample.cs: SetObjects
01private static void SetObjects() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer db = Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 Car car = new Car("BMW", new Pilot("Rubens Barrichello")); 08 db.Set(car); 09 car = new Car("Ferrari", new Pilot("Michael Schumacher")); 10 db.Set(car); 11 } 12 finally 13 { 14 db.Close(); 15 } 16 }
SerializeExample.vb: SetObjects
01Private Shared Sub SetObjects() 02 File.Delete(Db4oFileName) 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04 Try 05 Dim car As Car = New Car("BMW", New Pilot("Rubens Barrichello")) 06 db.Set(car) 07 car = New Car("Ferrari", New Pilot("Michael Schumacher")) 08 db.Set(car) 09 Finally 10 db.Close() 11 End Try 12 End Sub

We will save the database to XML file "formula1.xml":

SerializeExample.cs: ExportToXml
01private static void ExportToXml() 02 { 03 XmlSerializer carSerializer = new XmlSerializer(typeof(Car[])); 04 StreamWriter xmlWriter = new StreamWriter(XmlFileName); 05 IObjectContainer db = Db4oFactory.OpenFile(Db4oFileName); 06 try 07 { 08 IObjectSet result = db.Get(typeof(Car)); 09 Car[] cars = new Car[result.Size()]; 10 for (int i = 0; i < result.Size(); i++) 11 { 12 Car car = (Car)result[i]; 13 cars.SetValue(car,i); 14 } 15 carSerializer.Serialize(xmlWriter, cars); 16 xmlWriter.Close(); 17 } 18 finally 19 { 20 db.Close(); 21 } 22 }
SerializeExample.vb: ExportToXml
01Private Shared Sub ExportToXml() 02 Dim carSerializer As XmlSerializer = New XmlSerializer(GetType(Car())) 03 Dim xmlWriter As StreamWriter = New StreamWriter(XmlFileName) 04 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 05 Try 06 Dim result As IObjectSet = db.Get(GetType(Car)) 07 Dim cars() As Car = New Car(result.Size()) {} 08 Dim i As Integer 09 For i = 0 To result.Size() - 1 Step i + 1 10 Dim car As Car = CType(result(i), Car) 11 cars.SetValue(car, i) 12 Next 13 carSerializer.Serialize(xmlWriter, cars) 14 xmlWriter.Close() 15 Finally 16 db.Close() 17 End Try 18 End Sub

After the method executes all car objects from the database will be stored in the export file as an array. Note that child objects (Pilot) are stored as well without any additional settings. You can check the created XML file to see how it looks like.

Now we can clean the database and try to recreate it from the XML file:

SerializeExample.cs: ImportFromXml
01private static void ImportFromXml() 02 { 03 File.Delete(Db4oFileName); 04 XmlSerializer carSerializer = new XmlSerializer(typeof(Car[])); 05 FileStream xmlFileStream = new FileStream(XmlFileName, FileMode.Open); 06 Car[] cars = (Car[])carSerializer.Deserialize(xmlFileStream); 07 IObjectContainer db; 08 foreach (Car car in cars) 09 { 10 db = Db4oFactory.OpenFile(Db4oFileName); 11 try 12 { 13 Car newCar = (Car)car; 14 db.Set(newCar); 15 } 16 finally 17 { 18 db.Close(); 19 } 20 } 21 db = Db4oFactory.OpenFile(Db4oFileName); 22 try 23 { 24 IObjectSet result = db.Get(typeof(Pilot)); 25 ListResult(result); 26 result = db.Get(typeof(Car)); 27 ListResult(result); 28 } 29 finally 30 { 31 db.Close(); 32 } 33 }
SerializeExample.vb: ImportFromXml
01Private Shared Sub ImportFromXml() 02 File.Delete(Db4oFileName) 03 Dim carSerializer As XmlSerializer = New XmlSerializer(GetType(Car())) 04 Dim xmlFileStream As FileStream = New FileStream(XmlFileName, FileMode.Open) 05 Dim cars() As Car = CType(carSerializer.Deserialize(xmlFileStream), Car()) 06 Dim db As IObjectContainer 07 Dim car As Car 08 For Each car In cars 09 db = Db4oFactory.OpenFile(Db4oFileName) 10 Try 11 Dim newCar As Car = CType(car, Car) 12 db.Set(newCar) 13 Finally 14 db.Close() 15 End Try 16 Next 17 db = Db4oFactory.OpenFile(Db4oFileName) 18 Try 19 Dim result As IObjectSet = db.Get(GetType(Pilot)) 20 ListResult(result) 21 result = db.Get(GetType(Car)) 22 ListResult(result) 23 Finally 24 db.Close() 25 End Try 26 End Sub

Easy, isn't it? Obviously there is much more about XML serialization: renaming fields, storing collections, selective persistence etc. You should be able to find detailed description together with the serialization library, which you will use.