Click here to Skip to main content
Click here to Skip to main content

XML Serialization - Part 1

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

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

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

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 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)

Share

About the Author

Patel Pranav
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 Pinmembermuhamad tahir khan28-Jul-12 6:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by Patel Pranav
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid