Click here to Skip to main content
13,862,971 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 20 Feb 2008
Licenced CPOL

XML Serialization - Part 1

, 21 Feb 2008
Rate this:
Please Sign up or sign in to vote.
How to Use XML to Serialize and Deserialize an Object


System.Xml.Serialization provides methods for converting objects, including those based on custom classes, to and from XML files. With XML serialization, you can write almost any object to a text file for later retrieval with only a few lines of code. Similarly, you can use XML serialization to transmit objects between computers through Web services-even if the remote computer is not using the .NET Framework.


This example shows the simple way of using XML Serialization. With this concept, anyone can set the value to a specific object and store the object as an XML file to any location and retrieve the same file by other/same user at any time.

Using the Code

How to Use XML to Serialize an Object

Dim xs As XmlSerializer = New XmlSerializer(GetType(Order))
Dim sw As StreamWriter
sw = New StreamWriter(Application.StartupPath & "\Order\Order.xml")
Dim p_ord As New Order

    'Set properties
    p_ord.Customer = txtcustomer.Text
    p_ord.TableNo = txttableno.Text
    p_ord.MenuItem = txtmenuitem.Text
    p_ord.OrderedQty = txtqty.Text
    p_ord.OrderedTaste = cmbTaste.SelectedItem.ToString
    p_ord.OrderedFat = cmbFat.SelectedItem.ToString
    xs.Serialize(sw, p_ord)
    MsgBox("Order has been placed successfully.")
    TabControl1.SelectedTab = TabControl1.TabPages(1)
Catch ex As Exception
    p_ord = Nothing
End Try

How to Use XML to Deserialize an Object

Dim r_ord As Order
Dim sr As StreamReader
Dim xs As XmlSerializer = New XmlSerializer(GetType(Order))
    'Pull in contents of an object serialized into an XML file
    'and deserialize it into an object
    sr = New StreamReader(Application.StartupPath & "\Order\Order.xml")
    r_ord = xs.Deserialize(sr)
    ' Read the properties
    lblcustomer.Text = r_ord.Customer
    lbltableno.Text = r_ord.TableNo
    lblMenuitem.Text = r_ord.MenuItem
    lblqty.Text = r_ord.OrderedQty
    lblTaste.Text = r_ord.OrderedTaste
    lblFats.Text = r_ord.OrderedFat
Catch ex As Exception
    r_ord = Nothing
End Try

Output XML File

<?xml version="1.0" encoding="utf-8"?>
    <Class_Order xmlns:xsi= 

      <Customer>Bruce Preece</Customer>
      <MenuItem>Veg Cheese Burger</MenuItem>

Points of Interest

XML serialization cannot be used to serialize private data or object graphs. To serialize an object, first create a stream, TextWriter, or XmlWriter. Then create an XmlSerializer object and call the XmlSerializer.Serialize method. To deserialize an object, follow the same steps but call the XmlSerializer.Deserialize method.


Visit XML Serialization Part-2.

This article helps you brush up the advanced concepts of XML Serialization and abstract class as well.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Patel Pranav
Software Developer (Senior)
India India
Pranav Patel

I am serving my company as Sr. Software Engineer. I have more than 4 years of experience in Microsoft Technologies specially VB 6, VB.Net,C#,ASP.Net and MS SQL Server

You may also be interested in...

Comments and Discussions

QuestionXML Serialization Pin
muhamad tahir khan28-Jul-12 6:39
membermuhamad tahir khan28-Jul-12 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by Patel Pranav
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid