Solved

XML Serialization : Arraylist of a Class

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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