Click here to Skip to main content
15,882,017 members
Articles / Programming Languages / Visual Basic

XML Serialization - Part 1

Rate me:
Please Sign up or sign in to vote.
3.95/5 (6 votes)
21 Feb 2008CPOL1 min read 32K   405   19   1
How to Use XML to Serialize and Deserialize an Object
Image 1

Introduction

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.

Background

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

VB.NET
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
Try

    '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)
    
    sw.Close()
    
    MsgBox("Order has been placed successfully.")
    TabControl1.SelectedTab = TabControl1.TabPages(1)
Catch ex As Exception
    MsgBox(ex.ToString)
Finally
    p_ord = Nothing
End Try

How to Use XML to Deserialize an Object

VB.NET
Dim r_ord As Order
Dim sr As StreamReader
Dim xs As XmlSerializer = New XmlSerializer(GetType(Order))
Try
    '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)
    sr.Close()
    ' 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
    MsgBox(ex.ToString)
Finally
    r_ord = Nothing
End Try

Output XML File

XML
<?xml version="1.0" encoding="utf-8"?>
    <Class_Order xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Customer>Bruce Preece</Customer>
      <TableNo>4</TableNo>
      <OrderedQty>3</OrderedQty>
      <OrderedTaste>None</OrderedTaste>
      <OrderedFat>None</OrderedFat>
      <MenuItem>Veg Cheese Burger</MenuItem>
    </Class_Order>

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.

Updates

Visit XML Serialization Part-2.

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

License

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


Written By
Software Developer (Senior)
India India
Pranav Patel
B.Sc, MCSD

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

Comments and Discussions

 
QuestionXML Serialization Pin
muhamad tahir khan28-Jul-12 5:39
muhamad tahir khan28-Jul-12 5: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.