Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Serialization : Arraylist of a Class

Posted on 2007-03-30
3
Medium Priority
?
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

670 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