|
-
May 11th, 2006, 07:30 AM
#1
Web Service & Collection problem
Hi,
How can I serialize a class with collection property in a Web Service.
When I compile the app I get the message:"To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. Microsoft.VisualBasic.Collection does not implement Add(System.Object). "
Thanks
-
May 11th, 2006, 07:09 PM
#2
Re: Web Service & Collection problem
Ah, the good ol' collection object. Don't use that anymore, it's only there for making the VB6 migration easier and is not a native CLR member.
If you are using VB.NET 2002/2003, try using an ARRAYLIST instead.
If you are using VB.NET 2005 and you know the exact object type you will be storing in your array, try using a member of the System.Generic namespace, like the Generic.List().
Actually here is a list of all the functions that are not native CLR, and only exist to make migrating easier: http://msdn2.microsoft.com/en-us/lib...8f(d=ide).aspx
You should NOT be using any of the functions in the above link.
Good Luck,
Craig - CRG IT Solutions - Microsoft Gold Partner
-My posts after 08/2015 = .NET 4.x and Visual Studio 2015
-My posts after 11/2011 = .NET 4.x and Visual Studio 2012
-My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
-My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
-My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
-My posts before 04/2007 = .NET 1.1/2.0
*I do not follow all threads, so if you have a secondary question, message me.
-
June 15th, 2007, 01:22 PM
#3
Re: Web Service & Collection problem
This works! ROCK ON!--> Craig Gemmill
NO TO COLLECTIONS
YES TO ARRAYLISTS
I have business objects that contain collections that I needed to serialize to an xml string to store in a table then deserialize back to business object to continue processing. I've been trying for two days now to get them to serialize with no luck. If you convert the Collection to an ArrayList is works fine. All I had to change was in the Customer Class
Private _Orders as New Collection
changed to:
Private _Orders as New ArrayList
Other than that, the <xmlimports ... was a tough thing to get. This is my first real serialization project.
Here's a dummy app: you should be able to copy/paste. This will create a XML file called XMLSerialTest1.xml in the root of your project.
New Project
*********************************************
Customer Class
*********************************************
Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Collections.ObjectModel
<Serializable()> _
<XmlInclude(GetType(Order))> _
Public Class Customer
Private _FirstName As String
Private _LastName As String
Private _Orders As New ArrayList
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Public Property Orders() As ArrayList
Get
Return _Orders
End Get
Set(ByVal value As ArrayList)
_Orders = value
End Set
End Property
End Class
*********************************************
Order Class
*********************************************
Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Collections.ObjectModel
<Serializable()> _
Public Class Order
Private _OrderNumber As Integer
Private _OrderDescription As String
Private _Price As Integer
Public Property OrderNumber() As Integer
Get
Return _OrderNumber
End Get
Set(ByVal value As Integer)
_OrderNumber = value
End Set
End Property
Public Property OrderDescription() As String
Get
Return _OrderDescription
End Get
Set(ByVal value As String)
_OrderDescription = value
End Set
End Property
Public Property Price() As Integer
Get
Return _Price
End Get
Set(ByVal value As Integer)
_Price = value
End Set
End Property
End Class
*********************************************
Default.aspx
*********************************************
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Create Objects" />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Serialize Objects" /> <br />
<br />
<asp:Button ID="Button4" runat="server" Text="Deserialize XML" /><br />
<br />
<asp:Button ID="Button5" runat="server" Text="Show Output" /></div>
</form>
</body>
</html>
*********************************************
Default.aspx.vb
*********************************************
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Dim Customer1 As New Customer
Dim Order1 As New Order
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
With Customer1
.FirstName = "Micah"
.LastName = "Smith"
End With
With Order1
.OrderNumber = 1
.OrderDescription = "This is my first order to serialize."
.Price = 150
End With
Response.Write("Customer: FirstName-" & Me.Customer1.FirstName & " LastName-" & Me.Customer1.LastName)
Me.SaveObjects()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Me.Customer1 = New Customer
Me.Order1 = New Order
With Customer1
.FirstName = "Micah"
.LastName = "Smith"
End With
With Order1
.OrderNumber = 1
.OrderDescription = "This is my first order to serialize."
.Price = 150
End With
Customer1.Orders.Add(Order1)
Dim Order2 As New Order
With Order2
.OrderNumber = 2
.OrderDescription = "This is my second order to serialize."
.Price = 200
End With
Customer1.Orders.Add(Order2)
Dim oXS As XmlSerializer = New XmlSerializer(GetType(Customer))
Dim oStmW As System.IO.StreamWriter
oStmW = New System.IO.StreamWriter(Server.MapPath("XMLSerialTest1.xml"))
oXS.Serialize(oStmW, Customer1)
oStmW.Close()
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
Me.SaveObjects()
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim c1 As New Customer
Dim oDe As XmlSerializer = New XmlSerializer(GetType(Customer))
Dim xmlRead As XmlReader = New XmlTextReader(Server.MapPath("XMLSerialTest1.xml"))
If oDe.CanDeserialize(xmlRead) Then
c1 = CType(oDe.Deserialize(xmlRead), Customer)
Response.Write("*****" & c1.FirstName & "*****")
End If
End Sub
Protected Sub LoadObjects()
Me.Customer1 = Session("Customer")
End Sub
Protected Sub SaveObjects()
Session("Customer") = Me.Customer1
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|