Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 64,459 times

Contents

Downloads

Related Categories

XML Serialization in .NET - Just Add Water

t_o_p_ramen

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 Personobject. Its contents look like this:

<?xml version="1.0" encoding="utf-8"?>
<Class_Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property_Age>47</Property_Age>
<Property_Name>Ned Nederlander</Property_Name>
</Class_Person>

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:

dim oNed as Person
dim oStmR as StreamReader
'Pull in contents of an object serialized into an XML file
'and deserialize it into an object
oStmR = new StreamReader(Server.MapPath("ned.xml"))
oNed = oXS.Deserialize(oStmR)
oStmR.Close()
'Display property values
Response.Write("Hello() = " & oNed.Hello() & "<br />")
Response.Write("Goodbye() = " & oNed.Goodbye() & "<br />")

Before anything else, we declare a Person object and StreamReader object. 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 the 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 Hello() and 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.

Anthony Hart has been working in IT since 1995. Currently, he is managing Oneirasoft, LLC, a consulting and software business. In his free time (if there is such a thing) he enjoys composing music and writing.

Comments

  • Re: [3827] XML Serialization in .NET

    Posted by damightyz on 09 Aug 2007

    Fantastic Article!!


    Thank you so much for writing that article.  That was the first clearly stated explanation on serialization that I have come across and it helped me tremendously!

  • Re: [3827] XML Serialization in .NET

    Posted by vedics on 28 Jul 2006

    I have two text boxes in my .aspx form. At the click of a button I need to generate an xml with the following schema.


    <?xml version="1.0" encoding="utf-16"?>
    <ns0:Root xmlns:ns0...

  • Raisins

    Posted by dyfrgi on 14 Jan 2005

    What does this have to do with raisins? I see no raisins.

  • Good Article

    Posted by skeeterbug on 14 Jul 2003

    A good article for XML in .NET. This technology will truely standardize things, its a must for developers to learn and understand this.