By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

XML serialization of custom collection class in VB

P: n/a
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error

You must implement a default accessor on brWAP.clFile because it inherits from ICollectio

I must be having a thick day because I dont have a clue what the error message means. (I have written a defaut property called ITEM in the clFile class
'Classes to abstract a FILE entity that is persisted in the databas
Imports System.Xml.Serializatio
Imports dalWAP.dalWA

'A worker class to get the data from the Database and save it bac
Public Class egFil

Sub New(
End Su

'Get a collection of FILE entities from the databas
Public Function GetCollection(Optional ByVal FileID As Integer = -1) As clFil
Dim dsFiles As DataSe
Dim serializer As XmlSerialize

Dim oXML As Xml.XmlDataDocumen
Dim oclFile As clFil

Dim oXMLr As Xml.XmlNodeReade

'this is the line that gives the erro
'You must implement a default accessor on brWAP.clFile because it inherits from ICollection
serializer = New XmlSerializer(GetType(clFile)

'Call the data access layer to pysically read from the DB to a datase
dsFiles = GetFiles(FileID

'load the dataset into an xml documen
oXML = New Xml.XmlDataDocument(dsFiles

oXMLr = New Xml.XmlNodeReader(oXML
'deserialise the xml into a collection of FILE entitie
oclFile = CType(serializer.Deserialize(oXMLr), clFile

End Functio

End Clas

'Strongly Typed collection of FILE entitie
<XmlType(TypeName:="clFile")>
Public Class clFil
Inherits System.Collections.Specialized.NameObjectCollectio nBas

Public Sub New(
'default constructo
End Su

Public Function Add(ByVal value As cdFile) As Boolea
Me.BaseAdd("_" & value.FileID, value

End Functio

<XmlElementAttribute("cdFile", GetType(cdFile))>
Default Public Overloads ReadOnly Property Item(ByVal Key As String) As cdFil
Ge
Return Me.BaseGet("_" & Key
End Ge
End Propert

Public Function ItemAt(ByVal value As Integer) As cdFil
Return Me.BaseGet(value
End Functio

Public Function Remove(ByVal value As Integer) As cdFil
Dim ocdFile As cdFil
ocdFile = Me.Item(value
Me.BaseRemove("_" & value
Remove = ocdFil
ocdFile = Nothin
End Functio

Public Function RemoveAt(ByVal value As Integer) As cdFil
Dim ocdFile As cdFil
ocdFile = Me.ItemAt(value
Me.BaseRemoveAt(value
RemoveAt = ocdFil
ocdFile = Nothin
End Functio

End Clas

'<summary
'<para>Represents a FILE entity</para
'<para>A FILE entity implemnts a reference to an external file</para
'</summary><XmlType(TypeName:="cdFile")>
Public Class cdFil
Private mintFileID As Intege
Private mstrTheName As Strin
Private mstrThePath As Strin

Private mblnNew As Boolea
Private mblnDirty As Boolea
Private mblnDeleted As Boolea

Overrides Function GetHashCode() As Intege
Return mintFileID.GetHashCod
End Functio

'<summary
'<para>If record ID's are equal, objects are equal</para
'</summary
Overloads Function Equals(ByVal ID As Integer) As Boolea
If mblnNew The
Return Fals
Els
Return (ID = mintFileID
End I
End Functio

Overloads Function Equals(ByVal Test_cdFile As cdFile) As Boolea
If mblnNew The
Return Fals
Els
If Test_cdFile Is Nothing The
Return Fals
Els
Return (mintFileID = Test_cdFile.FileID
End I
End I

End Functio

'<summary
'<para>new instance of existing record</para
'</summary
Sub New(ByVal FileID As Long, ByVal TheName As String, ByVal ThePath As String
mintFileID = FileI
mstrTheName = TheNam
mstrThePath = ThePat
mblnDirty = False
mblnNew = True
End Sub

'<summary>
'<para>for a really new file</para>
'</summary>
Sub New(ByVal TheName As String, ByVal ThePath As String)
mstrTheName = TheName
mstrThePath = ThePath
mblnNew = True
mblnDirty = True

End Sub

'<summary>
'<para>empty constructor for xml de-serialisation</para>
'</summary>
Sub New()

mblnNew = False
mblnDirty = False
End Sub

Public Overloads Function Saved() As Boolean
mblnDirty = False
Return True
End Function

Public Overloads Function Saved(ByVal FileID As Long) As Boolean
mblnDirty = False
mblnNew = False
mintFileID = FileID
Return True
End Function

Public Function Delete() As Boolean
mblnDeleted = True
Return mblnDeleted
End Function
<XmlIgnore()> _
Public ReadOnly Property Dirty() As Boolean
Get
Return mblnDirty
End Get
End Property
<XmlIgnore()> _
Public ReadOnly Property Deleted() As Boolean
Get
Deleted = mblnDeleted
End Get
End Property
Public Property FileID() As Integer
Get
Return mintFileID
End Get
Set(ByVal Value As Integer)
mintFileID = Value
End Set
End Property

Public Property TheName() As String
Get
Return mstrTheName
End Get
Set(ByVal Value As String)
mstrTheName = Value
End Set
End Property

Public Property ThePath() As String
Get
Return mstrThePath
End Get
Set(ByVal Value As String)
mstrThePath = Value
End Set
End Property
End Class

Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Jamie,

I have just been struggling with the XmlSerializer myself.

I learnt that if your collection implements ICollection then you must have
an Add() method and an indexer (for C#) or an Item() method (for VB.NET).
Both of these must return the same Type e.g. cdFile. The indexer must accept
an Integer parameter.

So, you need to add another method to the clFile class:

Default Public Overloads ReadOnly Property Item(ByVal index As Integer) As
cdFile
Get
Return Me.BaseGetByIndex(index)
End Get
End Property

As I understand it, this is used to deserialize the collection.

I was not using NameObjectCollectionBase but saw messages from people having
problems with it and XmlSerializer.

"System.InvalidOperationException" Error While Serializing
NameValueCollection Objects Using XmlSerializer
http://support.microsoft.com/default...b;en-us;814187

If you get really stuck there is the IXmlSerializable interface that you can
implement.

--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"jamie_m_" <an*******@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
| I have a custom collection ... clFile that INHERITS from
NameObjectCollectionBase the problem is, when I try to create an
xmlserializer instance i get an error:
|
| You must implement a default accessor on brWAP.clFile because it inherits
from ICollection
|
| I must be having a thick day because I dont have a clue what the error
message means. (I have written a defaut property called ITEM in the clFile
class)
|
|
|
| 'Classes to abstract a FILE entity that is persisted in the database
| Imports System.Xml.Serialization
| Imports dalWAP.dalWAP
|
| 'A worker class to get the data from the Database and save it back
| Public Class egFile
|
| Sub New()
| End Sub
|
| 'Get a collection of FILE entities from the database
| Public Function GetCollection(Optional ByVal FileID As Integer = -1)
As clFile
| Dim dsFiles As DataSet
| Dim serializer As XmlSerializer
|
| Dim oXML As Xml.XmlDataDocument
| Dim oclFile As clFile
|
| Dim oXMLr As Xml.XmlNodeReader
|
| 'this is the line that gives the error
| 'You must implement a default accessor on brWAP.clFile because it
inherits from ICollection.
| serializer = New XmlSerializer(GetType(clFile))
|
| 'Call the data access layer to pysically read from the DB to a
dataset
| dsFiles = GetFiles(FileID)
|
| 'load the dataset into an xml document
| oXML = New Xml.XmlDataDocument(dsFiles)
|
| oXMLr = New Xml.XmlNodeReader(oXML)
| 'deserialise the xml into a collection of FILE entities
| oclFile = CType(serializer.Deserialize(oXMLr), clFile)
|
|
| End Function
|
| End Class
|
| 'Strongly Typed collection of FILE entities
| <XmlType(TypeName:="clFile")> _
| Public Class clFile
| Inherits System.Collections.Specialized.NameObjectCollectio nBase
|
|
| Public Sub New()
| 'default constructor
| End Sub
|
| Public Function Add(ByVal value As cdFile) As Boolean
| Me.BaseAdd("_" & value.FileID, value)
|
| End Function
|
| <XmlElementAttribute("cdFile", GetType(cdFile))> _
| Default Public Overloads ReadOnly Property Item(ByVal Key As
String) As cdFile
| Get
| Return Me.BaseGet("_" & Key)
| End Get
| End Property
|
| Public Function ItemAt(ByVal value As Integer) As cdFile
| Return Me.BaseGet(value)
| End Function
|
| Public Function Remove(ByVal value As Integer) As cdFile
| Dim ocdFile As cdFile
| ocdFile = Me.Item(value)
| Me.BaseRemove("_" & value)
| Remove = ocdFile
| ocdFile = Nothing
| End Function
|
| Public Function RemoveAt(ByVal value As Integer) As cdFile
| Dim ocdFile As cdFile
| ocdFile = Me.ItemAt(value)
| Me.BaseRemoveAt(value)
| RemoveAt = ocdFile
| ocdFile = Nothing
| End Function
|
|
| End Class
|
| '<summary>
| '<para>Represents a FILE entity</para>
| '<para>A FILE entity implemnts a reference to an external file</para>
| '</summary><XmlType(TypeName:="cdFile")> _
| Public Class cdFile
| Private mintFileID As Integer
| Private mstrTheName As String
| Private mstrThePath As String
|
| Private mblnNew As Boolean
| Private mblnDirty As Boolean
| Private mblnDeleted As Boolean
|
| Overrides Function GetHashCode() As Integer
| Return mintFileID.GetHashCode
| End Function
|
| '<summary>
| '<para>If record ID's are equal, objects are equal</para>
| '</summary>
| Overloads Function Equals(ByVal ID As Integer) As Boolean
| If mblnNew Then
| Return False
| Else
| Return (ID = mintFileID)
| End If
| End Function
|
| Overloads Function Equals(ByVal Test_cdFile As cdFile) As Boolean
| If mblnNew Then
| Return False
| Else
| If Test_cdFile Is Nothing Then
| Return False
| Else
| Return (mintFileID = Test_cdFile.FileID)
| End If
| End If
|
| End Function
|
| '<summary>
| '<para>new instance of existing record</para>
| '</summary>
| Sub New(ByVal FileID As Long, ByVal TheName As String, ByVal ThePath
As String)
| mintFileID = FileID
| mstrTheName = TheName
| mstrThePath = ThePath
| mblnDirty = False
| mblnNew = True
| End Sub
|
| '<summary>
| '<para>for a really new file</para>
| '</summary>
| Sub New(ByVal TheName As String, ByVal ThePath As String)
| mstrTheName = TheName
| mstrThePath = ThePath
| mblnNew = True
| mblnDirty = True
|
| End Sub
|
| '<summary>
| '<para>empty constructor for xml de-serialisation</para>
| '</summary>
| Sub New()
|
| mblnNew = False
| mblnDirty = False
| End Sub
|
| Public Overloads Function Saved() As Boolean
| mblnDirty = False
| Return True
| End Function
|
| Public Overloads Function Saved(ByVal FileID As Long) As Boolean
| mblnDirty = False
| mblnNew = False
| mintFileID = FileID
| Return True
| End Function
|
| Public Function Delete() As Boolean
| mblnDeleted = True
| Return mblnDeleted
| End Function
| <XmlIgnore()> _
| Public ReadOnly Property Dirty() As Boolean
| Get
| Return mblnDirty
| End Get
| End Property
| <XmlIgnore()> _
| Public ReadOnly Property Deleted() As Boolean
| Get
| Deleted = mblnDeleted
| End Get
| End Property
| Public Property FileID() As Integer
| Get
| Return mintFileID
| End Get
| Set(ByVal Value As Integer)
| mintFileID = Value
| End Set
| End Property
|
| Public Property TheName() As String
| Get
| Return mstrTheName
| End Get
| Set(ByVal Value As String)
| mstrTheName = Value
| End Set
| End Property
|
| Public Property ThePath() As String
| Get
| Return mstrThePath
| End Get
| Set(ByVal Value As String)
| mstrThePath = Value
| End Set
| End Property
| End Class
|
Nov 12 '05 #2

P: n/a
Hi Jamie,

I have just been struggling with the XmlSerializer myself.

I learnt that if your collection implements ICollection then you must have
an Add() method and an indexer (for C#) or an Item() method (for VB.NET).
Both of these must return the same Type e.g. cdFile. The indexer must accept
an Integer parameter.

So, you need to add another method to the clFile class:

Default Public Overloads ReadOnly Property Item(ByVal index As Integer) As
cdFile
Get
Return Me.BaseGetByIndex(index)
End Get
End Property

As I understand it, this is used to deserialize the collection.

I was not using NameObjectCollectionBase but saw messages from people having
problems with it and XmlSerializer.

"System.InvalidOperationException" Error While Serializing
NameValueCollection Objects Using XmlSerializer
http://support.microsoft.com/default...b;en-us;814187

If you get really stuck there is the IXmlSerializable interface that you can
implement.

--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"jamie_m_" <an*******@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
| I have a custom collection ... clFile that INHERITS from
NameObjectCollectionBase the problem is, when I try to create an
xmlserializer instance i get an error:
|
| You must implement a default accessor on brWAP.clFile because it inherits
from ICollection
|
| I must be having a thick day because I dont have a clue what the error
message means. (I have written a defaut property called ITEM in the clFile
class)
|
|
|
| 'Classes to abstract a FILE entity that is persisted in the database
| Imports System.Xml.Serialization
| Imports dalWAP.dalWAP
|
| 'A worker class to get the data from the Database and save it back
| Public Class egFile
|
| Sub New()
| End Sub
|
| 'Get a collection of FILE entities from the database
| Public Function GetCollection(Optional ByVal FileID As Integer = -1)
As clFile
| Dim dsFiles As DataSet
| Dim serializer As XmlSerializer
|
| Dim oXML As Xml.XmlDataDocument
| Dim oclFile As clFile
|
| Dim oXMLr As Xml.XmlNodeReader
|
| 'this is the line that gives the error
| 'You must implement a default accessor on brWAP.clFile because it
inherits from ICollection.
| serializer = New XmlSerializer(GetType(clFile))
|
| 'Call the data access layer to pysically read from the DB to a
dataset
| dsFiles = GetFiles(FileID)
|
| 'load the dataset into an xml document
| oXML = New Xml.XmlDataDocument(dsFiles)
|
| oXMLr = New Xml.XmlNodeReader(oXML)
| 'deserialise the xml into a collection of FILE entities
| oclFile = CType(serializer.Deserialize(oXMLr), clFile)
|
|
| End Function
|
| End Class
|
| 'Strongly Typed collection of FILE entities
| <XmlType(TypeName:="clFile")> _
| Public Class clFile
| Inherits System.Collections.Specialized.NameObjectCollectio nBase
|
|
| Public Sub New()
| 'default constructor
| End Sub
|
| Public Function Add(ByVal value As cdFile) As Boolean
| Me.BaseAdd("_" & value.FileID, value)
|
| End Function
|
| <XmlElementAttribute("cdFile", GetType(cdFile))> _
| Default Public Overloads ReadOnly Property Item(ByVal Key As
String) As cdFile
| Get
| Return Me.BaseGet("_" & Key)
| End Get
| End Property
|
| Public Function ItemAt(ByVal value As Integer) As cdFile
| Return Me.BaseGet(value)
| End Function
|
| Public Function Remove(ByVal value As Integer) As cdFile
| Dim ocdFile As cdFile
| ocdFile = Me.Item(value)
| Me.BaseRemove("_" & value)
| Remove = ocdFile
| ocdFile = Nothing
| End Function
|
| Public Function RemoveAt(ByVal value As Integer) As cdFile
| Dim ocdFile As cdFile
| ocdFile = Me.ItemAt(value)
| Me.BaseRemoveAt(value)
| RemoveAt = ocdFile
| ocdFile = Nothing
| End Function
|
|
| End Class
|
| '<summary>
| '<para>Represents a FILE entity</para>
| '<para>A FILE entity implemnts a reference to an external file</para>
| '</summary><XmlType(TypeName:="cdFile")> _
| Public Class cdFile
| Private mintFileID As Integer
| Private mstrTheName As String
| Private mstrThePath As String
|
| Private mblnNew As Boolean
| Private mblnDirty As Boolean
| Private mblnDeleted As Boolean
|
| Overrides Function GetHashCode() As Integer
| Return mintFileID.GetHashCode
| End Function
|
| '<summary>
| '<para>If record ID's are equal, objects are equal</para>
| '</summary>
| Overloads Function Equals(ByVal ID As Integer) As Boolean
| If mblnNew Then
| Return False
| Else
| Return (ID = mintFileID)
| End If
| End Function
|
| Overloads Function Equals(ByVal Test_cdFile As cdFile) As Boolean
| If mblnNew Then
| Return False
| Else
| If Test_cdFile Is Nothing Then
| Return False
| Else
| Return (mintFileID = Test_cdFile.FileID)
| End If
| End If
|
| End Function
|
| '<summary>
| '<para>new instance of existing record</para>
| '</summary>
| Sub New(ByVal FileID As Long, ByVal TheName As String, ByVal ThePath
As String)
| mintFileID = FileID
| mstrTheName = TheName
| mstrThePath = ThePath
| mblnDirty = False
| mblnNew = True
| End Sub
|
| '<summary>
| '<para>for a really new file</para>
| '</summary>
| Sub New(ByVal TheName As String, ByVal ThePath As String)
| mstrTheName = TheName
| mstrThePath = ThePath
| mblnNew = True
| mblnDirty = True
|
| End Sub
|
| '<summary>
| '<para>empty constructor for xml de-serialisation</para>
| '</summary>
| Sub New()
|
| mblnNew = False
| mblnDirty = False
| End Sub
|
| Public Overloads Function Saved() As Boolean
| mblnDirty = False
| Return True
| End Function
|
| Public Overloads Function Saved(ByVal FileID As Long) As Boolean
| mblnDirty = False
| mblnNew = False
| mintFileID = FileID
| Return True
| End Function
|
| Public Function Delete() As Boolean
| mblnDeleted = True
| Return mblnDeleted
| End Function
| <XmlIgnore()> _
| Public ReadOnly Property Dirty() As Boolean
| Get
| Return mblnDirty
| End Get
| End Property
| <XmlIgnore()> _
| Public ReadOnly Property Deleted() As Boolean
| Get
| Deleted = mblnDeleted
| End Get
| End Property
| Public Property FileID() As Integer
| Get
| Return mintFileID
| End Get
| Set(ByVal Value As Integer)
| mintFileID = Value
| End Set
| End Property
|
| Public Property TheName() As String
| Get
| Return mstrTheName
| End Get
| Set(ByVal Value As String)
| mstrTheName = Value
| End Set
| End Property
|
| Public Property ThePath() As String
| Get
| Return mstrThePath
| End Get
| Set(ByVal Value As String)
| mstrThePath = Value
| End Set
| End Property
| End Class
|
Nov 12 '05 #3

P: n/a
jwf
Hi Jamie,

When inheriting from a collectionBase style object, your constructor
must call MyBase.New( ) or attempting to serialize it will throw this
red-herring exception.

HTH,

Jordan

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.