Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

serialize arraylist

Posted on 2004-08-05
6
Medium Priority
?
1,122 Views
Last Modified: 2008-02-01

Look at the following code.  I found that when I had regular arrays, my code worked and serialization occured as normal, but when I tried to serialize the following class, which has an array list declaration in it,  the class did not serialize as expected.  

Any clues on how to serialize an arraylist?

See code below, and tell me how to serialize properly?

The reason I prefer arraylist over using regular arrays is the fact that removing an object becomes easy...
If I had to remove an object from a regular array, then I have to shift everything and re-dim...
If you know a better way then I would accept that answer as well....





<System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=True)> _
Public Class RunHistoryCollection

    Sub New()

    End Sub


    Private Const dimensioner As Integer = 20

    'You have to make it public for reading/writing xml data...
    'Public RunHistorys() As RunHistory        <-- I THINK IT LIKES THAT, BUT IT IS COMMENTED OUT

    Private RunHistorys As New ArrayList       <--  I THINK IT DOES NOT LIKE THAT


    Public ReadOnly Property Count()
        Get
            Return RunHistorys.Count
        End Get
    End Property



    Default Public ReadOnly Property Item(ByVal i As Integer) As RunHistory
        Get
            If i >= 0 And i < Me.Count Then

                Return RunHistorys(i)
            Else
                Return Nothing
            End If
        End Get
    End Property


    Public Sub Add(ByVal b As RunHistory)

        Me.RunHistorys.Add(b)

    End Sub



    Public Sub Remove(ByVal RemoveName As String, ByVal RemoveDate As Date)
        Dim ii As Integer

        Dim jj As Integer


        For ii = 0 To Me.Count - 1
            If Me.RunHistorys(ii).DateRan = RemoveDate And Me.RunHistorys(ii).RunName = RemoveName Then
                Me.RunHistorys.RemoveAt(ii)
                Exit For
            End If
        Next
    End Sub

    Public Sub Clear()

        Dim ii As Integer
        For ii = 0 To Me.Count - 1
            Me.RunHistorys.RemoveAt(0)
        Next
    End Sub
End Class


0
Comment
Question by:kamleshmistry
[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
6 Comments
 

Author Comment

by:kamleshmistry
ID: 11731612

I figured out the answer,
but the points are up for grabs, should anyone want to solve this riddle...
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 870 total points
ID: 11731623
its because there may be objects of many types and it doesn't know what to do with them http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542

from MSDN:

Serializing an ArrayList
The ArrayList class can contain a collection of diverse objects. You can therefore use an ArrayList much as you use an array. Instead of creating a field that returns an array of typed objects, however, you can create a field that returns a single ArrayList. However, as with arrays, you must inform the XmlSerializer of the types of objects the ArrayList contains. To accomplish this, assign multiple instances of the XmlElementAttribute to the field, as shown in the following example.

[Visual Basic]
Public Class Group
    <XmlElement(Type:=GetType(Employee)), _
    XmlElement(Type:=GetType(Manager))> _
    Public Info As ArrayList
End Class
[C#]
public class Group{
    [XmlElement(Type = typeof(Employee)),
    XmlElement(Type = typeof(Manager))]
    public ArrayList Info;
}
Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute
There are two attributes that can be applied to a class (and only a class): XmlRootAttribute and XmlTypeAttribute. These attributes are very similar. The XmlRootAttribute can be applied to only one class: the class that, when serialized, represents the XML document's opening and closing element — in other words, the "root" element. The XmlTypeAttribute, on the other hand, can be applied to any class, including the root class.

For example, in the previous examples, the Group class is the root class, and all its public fields and properties become the XML elements found in the XML document. Therefore, there can be only one root class. By applying the XmlRootAttribute, you can control the XML stream generated by the XmlSerializer. For example, you can change the element name and namespace.

The XmlTypeAttribute allows you to control the schema of the generated XML. This capability is useful when you need to publish the schema through an XML Web service. The following example applies both the XmlTypeAttribute and the XmlRootAttribute to the same class.

[Visual Basic]
<XmlRoot("NewGroupName"), _
XmlType("NewTypeName")> _
Public Class Group
    Public Employees() As Employee
End Class
[C#]
[XmlRoot("NewGroupName")]
[XmlType("NewTypeName")]
public class Group{
    public Employee[] Employees;
}
If this class is compiled, and the XML Schema Definition tool is used to generate its schema, you would find the following XML describing Group.

<xs:element name="NewGroupName" type="NewTypeName">
In contrast, if you were to serialize an instance of the class, only NewGroupName would be found in the XML document.

<NewGroupName>
    . . .
</NewGroupName>
0
 
LVL 7

Assisted Solution

by:mdamico
mdamico earned 300 total points
ID: 11731857
Add Serializable Attribute to the class

example:

<Serializable()>_
Pulbic Class Whatever
0
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!

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 total points
ID: 11735039
If you don't need XML then Binary Serialization does this automatically for you.

Idle_Mind
0
 

Author Comment

by:kamleshmistry
ID: 11739764
Idle Mind,

I hate binary serialization because it is not backwards compatible.

The answer was that any item you wish to serialize must be public to the class....
Nobody caught this...

Anyway, everybody gave good input...
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11739890
it does not have to be public, you can also implement IXmlSerializable to serialize it
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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