XML Serialization in ASP.NET, Part 2By Anthony Hart
In Part 1 we examined how to dehydrate (serialize) an object instance into an XML file. In this part we'll look at reversing the process - turning an XML file back into an object instance.
Just Add Water
Now let's look at this from the opposite angle. We've just seen how to serialize an object into an XML file and save it to disk, but now suppose we already had an XML file saved and wanted to use it to instantiate an object. In the downloadable code found at the end of this article, you will find a file called,
ned.xml. We’re going to use that XML file to create a
Its contents look like this:
You'll notice that this XML document has exactly the same structure as the XML file that we wrote to disk a moment ago but the data it contains is, of course, different. Now put on your wicked mad scientist grins and look at the code required to bring this beast of an object to life:
Before anything else, we declare a
Person object and
Next, we create an instance of the
StreamReader object and feed it the stored XML file.
Then we instantiate the
Person object by calling the
Deserialize() method of
XMLSerializer object. This method uses the
StreamReader object to read
the contents of the XML file and then instantiates an object whose state matches that described in the
XML file. Finally, we close up the
StreamReader object and then output the results of the
newly created object's
Goodbye() methods just to prove that it
was successfully created. It's just like that instant oatmeal Mom used to make.
Note: Something important to remember is that when an object is instantiated through Deserialization, its constructor is not called. Just keep that in mind if you plan on doing this with any objects which are very dependent on their constructors performing some crucial function.
Do I Have To Keep My Raisins?
Perhaps you are wondering now, "Pretty cool - but what if I don't want to save my object to disk?" Another good question. There's no reason you would have to. Let's suppose that for some reason, you needed to serialize an object into an XML string to be used for some purpose and then forgotten or re-instantiated or whatever else. This can be accomplished in almost the same way that was demonstrated earlier. However, instead of using a
StreamWriter object in the process, we
will use a
StringWriter object. See the code snippet below:
As you can see, we instantiate a new
Person object and
and then assign values to the
Age properties of the
object. We then call the
Serialize() method of the
XMLSerializer object and
Person object is serialized into an XML document and placed in the
Before we move on, it is important to understand some things about the
StreamWriter objects and Inheritance. The
Serialize() method of the
XMLSerializer object is an overloaded method and one of its signatures is:
Overloads Public Sub Serialize(TextWriter, Object). This means we must send it a
TextWriter object and some other object.
"Wait a minute!" I hear you shouting, "If it needs to be sent a
TextWriter object, why
are we sending it
StreamWriters?" That's because of
Inheritance. In object oriented development, objects can be derived from other objects, inheriting some
or all of the original object's characteristics. This is where
StreamWriter come from. They are "descendants" of
TextWriter. Think of it
this way: A man named Fritz Meyer has two children, Hansel and Gretel. Hansel is not Fritz, but he
is a Meyer as is Gretel and when they have a Meyer family reunion, Fritz, Hansel, and Gretel can
all get in the door because they are all Meyers. Similarly, because
StringWriter are both descended from
TextWriter, they can be used with this
StreamWriter doesn't have a way to
present its contents as a string data type, but
StringWriter does and we are interested,
at this point, in getting the XML string rather than saving it to a file. That is why, in the code
snippet above, we send a
Serialize() instead of a
StreamWriter. (For more information on inheritence and how it is used in .NET, be sure to
read: Using Object-Orientation in ASP.NET: Inheritance.)
After the serialization takes place, we capture the XML string by calling the
method of the
StringWriter object and placing the results in a string variable. Next, we
StringWriter object because we no longer need it. We now have our hands on the
XML string and can do with it what we please. In the downloadable example code, all we do with it is
output it to the browser.
As you have just seen, serialization is fairly easy to implement. I've already listed several possible reasons to use serialization in your applications and now that you know how to do it, I will leave the rest to your own capable imaginations. This article has focused only on how to serialize an object into an XML document, but please remember that objects can also be serialized into binary or SOAP formats. To learn more about those types of serialization, look up the BinaryFormatter class and the SOAPFormatter class.
Maybe this powerful technology didn't really have its humble beginnings in the bottom of a glass of powdered milk - but for some reason, it makes me smile to think so. Then again, maybe someday we'll see Bill Gates or one of his .NET guys sporting a liquidy white moustache on one of those "Got Milk" ads.