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
Solved

serialize arraylist

Posted on 2004-08-05
6
1,118 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Move a class from Solution Items to a project 2 33
Iteration Help (Asp.net VB) 5 24
Very Large data in MYSQL 7 73
Convert Ctime to date time in textfile? 7 32
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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