Solved

serialize arraylist

Posted on 2004-08-05
6
1,117 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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