Solved

XML Serialization : Arraylist of a Class

Posted on 2007-03-30
3
403 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

696 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