• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1125
  • Last Modified:

serialize arraylist


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
kamleshmistry
Asked:
kamleshmistry
3 Solutions
 
kamleshmistryAuthor Commented:

I figured out the answer,
but the points are up for grabs, should anyone want to solve this riddle...
0
 
gregoryyoungCommented:
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
 
mdamicoCommented:
Add Serializable Attribute to the class

example:

<Serializable()>_
Pulbic Class Whatever
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you don't need XML then Binary Serialization does this automatically for you.

Idle_Mind
0
 
kamleshmistryAuthor Commented:
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
 
gregoryyoungCommented:
it does not have to be public, you can also implement IXmlSerializable to serialize it
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now