234
Chapter 12 Persistent Objects: Serialization and Relational Databases
public void DeSerialize()
{
Person[] myRestoredPeople;
XmlSerializer mySerializer = new XmlSerializer(typeof(Person[]));
TextReader myReader = new StreamReader(“person.xml”);
myRestoredPeople = (Person[])mySerializer.Deserialize(myReader);
Console.WriteLine(“My People restored:”);
foreach (Person listPerson in myRestoredPeople)
{
Console WriteLine(listPerson.Name + “ is “ +
listPerson.Age + “ years old.”);
}
Console.WriteLine(“Press any key to continue...”);
Console.ReadKey();
}
Note that we iterate through a data structure using a foreach loop.The complete code
for this C# example and the corresponding VB .NET code is listed at the end of this
chapter.
As we have noted, one of the major advantages of this approach is that the XML file is
accessible by any and all languages and platforms that implement the XML interface, in-
cluding Java.Although we implemented the Java example in a proprietary way, this was
done for example purposes.There is nothing stopping a programmer from using the XML
approach in Java as well.
Writing to a Relational Database
The relational database is perhaps one of the most important tools ever devised in the in-
formation technology field.Although some people might not buy into this statement
completely, and there certainly are many other important candidates, the relational data-
base has had a huge impact on the IT industry. In fact, the relational database remains a
powerhouse despite the fact that other technologies may well be technologically better.
The reason for this is that relational databases are the database of choice for most busi-
nesses today. From Oracle to SQLServer in the large applications, to Microsoft Access in
small to medium applications, relational databases are everywhere.
Although relational databases are a wonderful technology, they provide a bit of a prob-
lem when it comes to interfacing with objects. Just as with the issue of writing to a flat
file, taking an object that may be composed of other objects and writing it to relational
databases, which are not designed in an object-oriented manner, can be problematic.
Relational databases are built on the concept of tables. Figure 12.4 shows a typical Mi-
crosoft Access table relationship.This relational model is so widespread that many people
intuitively think of all data models in this way. However, the object-oriented model is not
table-driven. Figure 12.4 shows the familiar Northwind relational database model that
ships with Microsoft Access.
Because objects do not map conveniently to tables, object-oriented database systems
were developed in the 1990s.An interesting bit of history is that although these databases