Solved

serialize arraylist

Posted on 2004-08-05
6
1,115 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
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 290 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 100 total points
ID: 11731857
Add Serializable Attribute to the class

example:

<Serializable()>_
Pulbic Class Whatever
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 85

Assisted Solution

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now