Library tutorials & articles

XML Serialization in .NET

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.

Comments

  1. 09 Aug 2007 at 15:58

    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!

    Kudos to you my friend!

    Jason Z

  2. 28 Jul 2006 at 11:52

    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="http://Demo123.Schm_In">
      <No_1>      Contents of textBox 1       </No_1>
      <No_2>      Contents of textBox 2       </No_2>
    </ns0:Root>



    Now i need to post this through hppt.

    Pls give me the code in C#

  3. 14 Jan 2005 at 08:33
    What does this have to do with raisins? I see no raisins.
  4. 14 Jul 2003 at 11:28

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

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of XML Serialization in .NET.

Leave a comment

Sign in or Join us (it's free).

Anthony Hart 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 ...

Related discussion

Related podcasts

  • Episode 10: LINQ

    K Scott leads us in a discussion of LINQ, including: What is it How introducing LINQ to .NET changed the framework LINQ Providers LINQ to XML LINQ to SQL - how it's different from EF, tips and tricks, when to use it Links: LINQpad 3rd Party LIN...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

We'd love to hear what you think! Submit ideas or give us feedback