Solved

XML Serialization : Arraylist of a Class

Posted on 2007-03-30
3
390 Views
Last Modified: 2012-06-21
Hi, my name is John and I'm a VB.net 2005 Newbie.    Here's my problem:

I'm creating a VB.net 2005 program that allows the user to build a sequence of machining operations.   I have a class called cOperation, which contains all the parameters for a single CNC machining operation (Drill, Countour, or Pocket, ...etc.)   I'm using an arraylist called OpList to contain each of machining operation defined in  the Operation Class (cOperation).


EX:
<Serializable()> Public Class cOperation
    Public OpName As String         ' Name of Operation (Drill, Mill, PktRect, etc.)
    Public Depth As Double              ' Depth in z-axis for operation
    Public MaxDepthCut As Double    ' Maximum cutter depth per pass
    Public ToolNo As Integer              ' Tool Number in Magazine
    Public Pts As New ArrayList      ' Arraylist of Pts that define geometry or location to drill
    Public Contour As New ArrayList   ' Arraylist of contour class (line or arc with endpoints)
    Public Radius As Double
ETC...


Now, I want to save the list of machining operations to a text file.   I've just stumbled upon XML serialization/deserialization.   It seems to be perfect, but I can't get it to work.   The examples I've found on the Web are either outdated or don't work.

Could someone please provide a simple example?
0
Comment
Question by:jlaidlaw
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 18824252
"I'm creating a VB.net 2005 program"

Don't use ArrayLists...use the Generic List.

In your cOperation class, replace all ArrayList with List:

    Public Pts As New List(Of Point)
    Public Contours As New List(Of Contour)

Then instead of:

    Private OpList As New ArrayList()

Use:

    Private OpList As New List(Of cOperation)

Here is a simplified example of what you are doing:

Imports System.IO
Imports System.Xml.Serialization

Public Class Form1

    Public Class cOperation
        Public OpName As String         ' Name of Operation (Drill, Mill, PktRect, etc.)
        Public Depth As Double              ' Depth in z-axis for operation
        Public MaxDepthCut As Double    ' Maximum cutter depth per pass
        Public ToolNo As Integer              ' Tool Number in Magazine
        Public Pts As New List(Of Point)      ' List of Pts that define geometry or location to drill
        Public Radius As Double
    End Class

    Private OpList As New List(Of cOperation)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cop As New cOperation
        cop.OpName = "Drill"
        cop.Depth = 0.5
        cop.MaxDepthCut = 1
        cop.ToolNo = 4
        cop.Pts.Add(New Point(0, 0))
        cop.Pts.Add(New Point(25, 25))
        cop.Radius = 0.3
        OpList.Add(cop)

        cop = New cOperation
        cop.OpName = "Pocket"
        cop.Depth = 0.75
        cop.MaxDepthCut = 5
        cop.ToolNo = 1
        cop.Pts.Add(New Point(10, 10))
        cop.Pts.Add(New Point(7, 2))
        cop.Radius = 0.9
        OpList.Add(cop)

        Dim fileName As String = "c:\myData.xml"

        ' serialize it
        Dim xml As XmlSerializer = New XmlSerializer(OpList.GetType)
        Dim writer As New FileStream(fileName, FileMode.OpenOrCreate)
        xml.Serialize(writer, OpList)
        writer.Close()

        ' deserialize it
        Dim reader As New FileStream(fileName, FileMode.OpenOrCreate)
        OpList = xml.Deserialize(reader)
        reader.Close()

        ' show that it worked
        For Each cop In OpList
            Debug.Print(cop.OpName)
            For Each pt As Point In cop.Pts
                Debug.Print(pt.ToString)
            Next
        Next
    End Sub

End Class


And here is the XML file it generated:
  <?xml version="1.0" ?>
- <ArrayOfCOperation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <cOperation>
  <OpName>Drill</OpName>
  <Depth>0.5</Depth>
  <MaxDepthCut>1</MaxDepthCut>
  <ToolNo>4</ToolNo>
- <Pts>
- <Point>
  <X>0</X>
  <Y>0</Y>
  </Point>
- <Point>
  <X>25</X>
  <Y>25</Y>
  </Point>
  </Pts>
  <Radius>0.3</Radius>
  </cOperation>
- <cOperation>
  <OpName>Pocket</OpName>
  <Depth>0.75</Depth>
  <MaxDepthCut>5</MaxDepthCut>
  <ToolNo>1</ToolNo>
- <Pts>
- <Point>
  <X>10</X>
  <Y>10</Y>
  </Point>
- <Point>
  <X>7</X>
  <Y>2</Y>
  </Point>
  </Pts>
  <Radius>0.9</Radius>
  </cOperation>
  </ArrayOfCOperation>
0
 

Author Comment

by:jlaidlaw
ID: 18824623
BINGO....That's IT!!!

Although,  I'm not really sure what the difference is between Arraylist and List(of...).  

This is SO much easier than my tentative plan to write each line to a text file with a line entry declaring the number of points or contours to read.   Not only would it be tedious, it's still vulnerable to crashing or reading in bad data.

THANK-YOU!!!!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18824689
You're welcome.   =)

Read up here on Generics:
http://msdn2.microsoft.com/en-us/library/ms172193.aspx

A List is a Generic version of an ArrayList:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

In general, List will be faster than ArrayList.  Since the List is strongly typed you can serialize it.  An ArrayList can hold anything so the serializer doesn't know what to do with it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now