Click here to Skip to main content
13,558,383 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 21 Feb 2008
Licenced CPOL

XML_Serialization - Part 2

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


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

Using the Code

Orderlist class contains two main modules; SaveOrder which is used to serialize the object and generate the XML file. ReadOrder is a shared function which is used to deserialize it into an object.

' Save the Order into the xml file.
    Public Sub SaveOrder()
            Dim xs As XmlSerializer = New XmlSerializer(GetType(OrderList))
            Dim sw As StreamWriter
            sw = New StreamWriter(Application.StartupPath & "\Order\Order.xml")
            xs.Serialize(sw, Me)
        Catch ex As Exception
        End Try
    End Sub

    Public Shared Function ReadOrder() As OrderList
        'use shared keyword for access this function directly for Orderlist object.
        Dim sr As StreamReader
        Dim xs As XmlSerializer = New XmlSerializer(GetType(OrderList))
            '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")
            Dim r_order As OrderList = DirectCast(xs.Deserialize(sr), OrderList)
            Return r_order
        Catch ex As Exception
            Return Nothing
        End Try
    End Function 

In this example, I have used the DataGridView control to display the placedorder which is stored in an XML file. To display data in DataGridView, I have created a DataTable and created the DataColumns according to PlacedOrder. Using the collection of Placedorders, I have added data one in DataTable using DataRow.

Private Sub cmdReadOrder_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles cmdReadOrder.Click

        Dim r_orderList As OrderList = OrderList.ReadOrder()

        ' If we succeeded, display the Saved Order.
        If Not (r_orderList Is Nothing) Then
            m_OrderList = r_orderList
        End If
        Dim dt As New DataTable
        Dim dr As DataRow


        For Each ord As Order In r_orderList.Orders
            dr = dt.NewRow
            dr("CUSTOMER") = ord.m_Customer
            dr("TABLENO") = ord.m_TableNo
            dr("MENUITEM") = ord.m_menuitem
            dr("QTY") = ord.m_OrderQty
            dr("TASTE") = ord.m_Taste
            dr("FAT") = ord.m_Fat

        DataGridView1.DataSource = dt
        DataGridView1.Columns("CUSTOMER").Width = 95
        DataGridView1.Columns("TABLENO").Width = 60
        DataGridView1.Columns("QTY").Width = 40
        DataGridView1.Columns("TASTE").Width = 60
        DataGridView1.Columns("FAT").Width = 60

    End Sub  


Visit XML Serialization - Part 1.

I have submitted XML Serialization Part I. In the XML Serialization - Part 1 article, you can serialize only one instance of an object. If the user wishes to pass through more than one order, he has to create a separate XML file for each placed Order.

XML Serialization - Part 2 removes this constraint. You can place multiple orders with different values and create a list of it. You can even remove your placed order before you do serialization.


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 Pin
stevenson_john7-Jun-12 19:43
memberstevenson_john7-Jun-12 19:43 

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 | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by Patel Pranav
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid