473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Polymorphic XML Serialized Array


Hello Folks

I am trying to create an xml file from a class containing a
polymorphic array. The example: AutoCompany contains an array of
Vehicles(base class) which can either be Cars or Trucks(derived classes).
Everything works accept the XMLSerialization. The error is very vague. The
simplified code follows and the entire code can be downloaded from a
compressed file from http://www.Cartner.Net/PolymorphismExample.zip

Please let me know what is causing this. I have not found any
examples on the internet that address this specific problem. Thank you for
your help in advance.

Best Regards
Todd

-------------------------------------------------------------------
Option Explicit On
Option Strict On

Imports System.IO
Imports System.Xml.Serialization

Public Class frmMain
Inherits System.Windows.Forms.Form

Private GeneralMotors As clsAutoCompany

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
GeneralMotors.ToString()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim XSer As XmlSerializer = New XmlSerializer(GetType(clsAutoCompany))
Dim FIO As TextWriter = New StreamWriter("PolyTest.xml")

' ********** Error occurs on the next line **********
XSer.Serialize(FIO, Me.GeneralMotors)
FIO.Close()
End Sub
End Class
-------------------------------------------------------------------
Option Explicit On
Option Strict On

<Serializable()> Public Class clsAutoCompany
Public Vehicles() As clsVehicle

Public Sub New()
Dim VT As clsTruck
Dim VC As clsCar
ReDim Vehicles(3)
VT = New clsTruck
VT.Index = 0
VT.TruckType = "SUV"
Vehicles(0) = VT
VC = New clsCar
VC.Index = 1
VC.CarType = "Sports"
Vehicles(1) = VC
VC = New clsCar
VC.Index = 2
VC.CarType = "Luxury"
Vehicles(2) = VC
VT = New clsTruck
VT.Index = 3
VT.TruckType = "4X4"
Vehicles(3) = VT
End Sub

Public Overrides Function ToString() As String
Dim Counter As Int32
For Counter = 0 To Vehicles.Length - 1
Debug.WriteLine(Vehicles(Counter).ToString & " - " &
Vehicles(Counter).Index.ToString)
Next
End Function
End Class
-------------------------------------------------------------------
Option Explicit On
Option Strict On

<Serializable()> Public MustInherit Class clsVehicle
Public Index As Int32

Public Sub New()
Index = -1
End Sub

Public MustOverride Shadows Function ToString() As String
End Class

<Serializable()> Public Class clsCar
Inherits clsVehicle

Public CarType As String

Public Overrides Function ToString() As String
Dim Output As String
Output = "Car Type = " & CarType
Return Output
End Function
End Class

<Serializable()> Public Class clsTruck
Inherits clsVehicle

Public TruckType As String

Public Overrides Function ToString() As String
Dim Output As String
Output = "Truck Type = " & TruckType
Return Output
End Function
End Class
-------------------------------------------------------------------

Best Regards
Todd
Nov 21 '05 #1
2 1437

Would have helped if you posted the error message, even if it is
vague. With XmlSerialization the errors usually have InnerExceptions
(often nested several levels deep) and the inner-most exception is
what provides the real information about what's wrong.

From looking at your code, the problem is most likely related to:

1. clsVehicle is mustinherit. Not sure you can xml serialize a class
with this attribute set. It goes against the public constructor
requirement, but perhaps if there are no actual vehicle instances it
won't cause any problems (and there can't be since the class is
abstract).

2. Need XmlInclude attributes to indicate the sub-classes that should
also be included in the serialization type mapping.

3. Not that it will cause any errors, but note that Serializable
attribute has nothing to do with XmlSerialization--it's related to
binary serialization.

4. Not that it will cause any errors, but those prefixes are very old
school (clsVehicle should just be Vehicle). Of course you can do
whatever you want and name things however you want, but the standard
practice with VB.NET is to not use prefixes.

HTH,

Sam

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #2

As I said, you need the last InnerException. Catch the error, walk
through the InnerExceptions, and you'll get more info.

Did you implement the items I mentioned in my previous post and did
they not address the problem?

Sam

On Wed, 30 Mar 2005 19:23:53 -0500, "Todd Cartner"
<To**********@Cartner.Net> wrote:

Here is the incredibly helpful and descriptive error message...


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns...
7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
1
by: Michal Piatkowski | last post by:
I have a problem with serialization of a polymorphic array. It has attributes defined XmlArray and XmlArrayItem for XML serialization. The attribute specifies Type1 but as my array is polymorphic...
7
by: John Grandy | last post by:
make a call to XML Web Service WebMethod ... returns object myArray with no error ... myArray contains objects of type StringKeyStringValue runtime error occurs on accessing properties of...
2
by: 6kjfsyg02 | last post by:
I am trying to return one of two different objects from the same method, but I can not do it in WSDL or C#. I have a web service with three methods. I have been told that one of the methods...
5
by: Ben Kim | last post by:
Hello all, We are diving into VB.NET 2005 head first, coming from a language called Clarion. What are some of the drawbacks, if any, to serialized objects? Should we just use streaming data? ...
11
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public:...
1
by: Joris van Lier | last post by:
Hi, im trying to validate objects before they are sent over the wire to a webservice, the schema embedded into WSDL is not sufficient so i took that schema and extended it with additional...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.