Solved

XML Serialization : Arraylist of a Class

Posted on 2007-03-30
3
411 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 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

615 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