|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Polymorphic XML Serialized ArrayI 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 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. Here is the incredibly helpful and descriptive error message...
Show quoteHide quote "Samuel R. Neff" <blinex@newsgroup.nospam> wrote in message news:6e7m4153tgj1bqiptv7v7ccaa260psene7@4ax.com... > > 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. 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" <Todd.Cart***@Cartner.Net> wrote: > B-Line is now hiring one Washington D.C. area VB.NET > Here is the incredibly helpful and descriptive error message... > > > developer for WinForms + WebServices position. Seaking mid to senior level developer. For information or to apply e-mail resume to sam_blinex_com.
Mixing VB variables with standard text to output text file
Refresh DataSet - please Help Me :-( Get Variable Out of Arraylist ReInstantiate an Object? Advice needed using StringBuilder class for concatenation? foxpro table hosed up when called 2nd time from VB.net Creating array on the fly as a parameter to a subroutine? SaveFileDialog how to convert... |
|||||||||||||||||||||||