Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 64,007 times

Contents

Downloads

Related Categories

XML Serialization in .NET - Do I Have to Keep My Raisins?

t_o_p_ramen

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:

dim oDusty as new Person()
dim oStrW as new StringWriter()
dim sXML as string

'Set properties
oDusty.Name = "Dusty Bottoms"
oDusty.Age = 51

'Serialize object into an XML string
oXS.Serialize(oStrW, oDusty)
sXML = oStrW.ToString()
oStrW.Close()

As you can see, we instantiate a new Person object and StringWriter object and then assign values to the Name and Age properties of the Person object. We then call the Serialize() method of the XMLSerializer object and the Person object is serialized into an XML document and placed in the StringWriter object.

Before we move on, it is important to understand some things about the StringWriter and 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 StringWriters and 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 StringWriter and 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 StreamWriter and StringWriter are both descended from TextWriter, they can be used with this call to Serialize(). Unfortunately, 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 StringWriter to Serialize() instead of a StreamWriter.

After the serialization takes place, we capture the XML string by calling the ToString() method of the StringWriter object and placing the results in a string variable. Next, we close the 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.

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.