471,056 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XmlSerializer instantiation error

Hello,
I hope this is the right group to post this to. I'm trying to serialize a
class I've written and I'd like to be able to serialze to both binary and
xml formats. Binary serialization is working fine but when I try to
instantiate an XmlSerializer object with:

Dim xmls As New XmlSerializer(GetType(CLayerDefinition))

I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll

Additional information: There was an error reflecting type
'xmlSerializeTest.CLayerDefinition'.

Can anybody give me some pointer as to why this might be happening since the
error message is not too helpful?

Thanks Steve

The class CLayerDefinition is laid out as:

<Serializable()> _
Public Class CLayerDefinition
Implements IDisposable, IComparable

#Region "Private simple objects"
Private mThemeID As Integer
Private mLayerName As String
Private mThemeName As String
Private mLocation As String
Private mDraw As Boolean
Private mMinScale As Integer
Private mMaxScale As Integer
Private mUserDefined As Boolean
Private mSaved As Boolean
Private mDrawOrder As Integer
Private mLegendType As Integer
Private mSymbolType As Integer
Private mLabelField As String
Private mHasImages As Boolean
Private mRelateTable As String
Private mTableLocation As String
Private mRelateField As String
Private mFileNameField As String
Private mClassItemField As String
Private mNumberOfBreaks As Integer
#End Region

#Region "Private complex objects"
<XmlArray()> _
Private mSymbols() As CLayerSymbol
#End Region

#Region "Public properties"
Public Property NumberOfBreaks() As Integer
Get
Return mNumberOfBreaks
End Get
Set(ByVal Value As Integer)
mNumberOfBreaks = Value
End Set
End Property

Public Property ClassItemField() As String
Get
Return mClassItemField
End Get
Set(ByVal Value As String)
mClassItemField = Value
End Set
End Property

Public Property HasImages() As Boolean
Get
Return mHasImages
End Get
Set(ByVal Value As Boolean)
mHasImages = Value
End Set
End Property

Public Property FileNameField() As String
Get
Return mFileNameField
End Get
Set(ByVal Value As String)
mFileNameField = Value
End Set
End Property

Public Property RelateField() As String
Get
Return mRelateField
End Get
Set(ByVal Value As String)
mRelateField = Value
End Set
End Property

Public Property TableLocation() As String
Get
Return mTableLocation
End Get
Set(ByVal Value As String)
mTableLocation = Value
End Set
End Property

Public Property RelateTable() As String
Get
Return mRelateTable
End Get
Set(ByVal Value As String)
mRelateTable = Value
End Set
End Property

Public Property LabelField() As String
Get
Return mLabelField
End Get
Set(ByVal Value As String)
mLabelField = Value
End Set
End Property

Public Property SymbolType() As Integer
Get
Return mSymbolType
End Get
Set(ByVal Value As Integer)
mSymbolType = Value
End Set
End Property

Public Property LegendType() As Integer
Get
Return mLegendType
End Get
Set(ByVal Value As Integer)
mLegendType = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property LayerName() As String
Get
Return mLayerName
End Get
Set(ByVal Value As String)
mLayerName = Value
End Set
End Property

Public Property ThemeName() As String
Get
Return mThemeName
End Get
Set(ByVal Value As String)
mThemeName = Value
End Set
End Property

Public Property Location() As String
Get
Return mLocation
End Get
Set(ByVal Value As String)
mLocation = Value
End Set
End Property

Public Property DrawOrder() As Integer
Get
Return mDrawOrder
End Get
Set(ByVal Value As Integer)
mDrawOrder = Value
End Set
End Property

Public Property Draw() As Boolean
Get
Return mDraw
End Get
Set(ByVal Value As Boolean)
mDraw = Value
End Set
End Property

Public Property MinScale() As Integer
Get
Return mMinScale
End Get
Set(ByVal Value As Integer)
mMinScale = Value
End Set
End Property

Public Property MaxScale() As Integer
Get
Return mMaxScale
End Get
Set(ByVal Value As Integer)
mMaxScale = Value
End Set
End Property

Public ReadOnly Property UserDefined() As Boolean
Get
Return mUserDefined
End Get
End Property

Public Property Saved() As Boolean
Get
Return mSaved
End Get
Set(ByVal Value As Boolean)
mSaved = Value
End Set
End Property

Public Property Symbols() As CLayerSymbol()
Get
Return mSymbols
End Get
Set(ByVal Value As CLayerSymbol())
mSymbols = Value
End Set
End Property
#End Region

#Region "Public member methods"
Public Sub Dispose() Implements System.IDisposable.Dispose
CleanUp()
GC.SuppressFinalize(Me)
End Sub

Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo
If TypeOf obj Is CLayerDefinition Then
Dim cld As CLayerDefinition = DirectCast(obj, CLayerDefinition)

mDrawOrder.CompareTo(cld.mDrawOrder)
End If

Throw New ArgumentException("obj is not a CLayerDefintion type")
End Function

#End Region

#Region "Private member methods"
Private Sub CleanUp()
If Not mSymbols Is Nothing Then
If mSymbols.GetUpperBound(0) >= 0 Then
Erase mSymbols
End If
mSymbols = Nothing
End If
End Sub
#End Region

#Region "Constructors"
Public Sub New()
mUserDefined = False
End Sub

Public Sub New(ByVal usrDefined As Boolean)
mUserDefined = usrDefined
End Sub
#End Region
End Class
Nov 12 '05 #1
4 1838

Steve,

Did you read through [0]?

It explains a number of scenarios that can cause this error.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0]
http://msdn.microsoft.com/XML/Buildi...k/default.aspx
?pull=/library/en-us/dnxmlnet/html/trblshtxsd.asp
-----Original Message-----
From: Steve Long [mailto:St**********@NoSpam.com]
Posted At: Tuesday, February 01, 2005 3:20 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer instantiation error
Subject: XmlSerializer instantiation error

Hello,
I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is working fine but when I try to
instantiate an XmlSerializer object with:

Dim xmls As New XmlSerializer(GetType(CLayerDefinition))

I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: There was an error reflecting type
'xmlSerializeTest.CLayerDefinition'.

Can anybody give me some pointer as to why this might be happening since the
error message is not too helpful?

Thanks Steve

The class CLayerDefinition is laid out as:

<Serializable()> _
Public Class CLayerDefinition
Implements IDisposable, IComparable

#Region "Private simple objects"
Private mThemeID As Integer
Private mLayerName As String
Private mThemeName As String
Private mLocation As String
Private mDraw As Boolean
Private mMinScale As Integer
Private mMaxScale As Integer
Private mUserDefined As Boolean
Private mSaved As Boolean
Private mDrawOrder As Integer
Private mLegendType As Integer
Private mSymbolType As Integer
Private mLabelField As String
Private mHasImages As Boolean
Private mRelateTable As String
Private mTableLocation As String
Private mRelateField As String
Private mFileNameField As String
Private mClassItemField As String
Private mNumberOfBreaks As Integer
#End Region

#Region "Private complex objects"
<XmlArray()> _
Private mSymbols() As CLayerSymbol
#End Region

#Region "Public properties"
Public Property NumberOfBreaks() As Integer
Get
Return mNumberOfBreaks
End Get
Set(ByVal Value As Integer)
mNumberOfBreaks = Value
End Set
End Property

Public Property ClassItemField() As String
Get
Return mClassItemField
End Get
Set(ByVal Value As String)
mClassItemField = Value
End Set
End Property

Public Property HasImages() As Boolean
Get
Return mHasImages
End Get
Set(ByVal Value As Boolean)
mHasImages = Value
End Set
End Property

Public Property FileNameField() As String
Get
Return mFileNameField
End Get
Set(ByVal Value As String)
mFileNameField = Value
End Set
End Property

Public Property RelateField() As String
Get
Return mRelateField
End Get
Set(ByVal Value As String)
mRelateField = Value
End Set
End Property

Public Property TableLocation() As String
Get
Return mTableLocation
End Get
Set(ByVal Value As String)
mTableLocation = Value
End Set
End Property

Public Property RelateTable() As String
Get
Return mRelateTable
End Get
Set(ByVal Value As String)
mRelateTable = Value
End Set
End Property

Public Property LabelField() As String
Get
Return mLabelField
End Get
Set(ByVal Value As String)
mLabelField = Value
End Set
End Property

Public Property SymbolType() As Integer
Get
Return mSymbolType
End Get
Set(ByVal Value As Integer)
mSymbolType = Value
End Set
End Property

Public Property LegendType() As Integer
Get
Return mLegendType
End Get
Set(ByVal Value As Integer)
mLegendType = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property LayerName() As String
Get
Return mLayerName
End Get
Set(ByVal Value As String)
mLayerName = Value
End Set
End Property

Public Property ThemeName() As String
Get
Return mThemeName
End Get
Set(ByVal Value As String)
mThemeName = Value
End Set
End Property

Public Property Location() As String
Get
Return mLocation
End Get
Set(ByVal Value As String)
mLocation = Value
End Set
End Property

Public Property DrawOrder() As Integer
Get
Return mDrawOrder
End Get
Set(ByVal Value As Integer)
mDrawOrder = Value
End Set
End Property

Public Property Draw() As Boolean
Get
Return mDraw
End Get
Set(ByVal Value As Boolean)
mDraw = Value
End Set
End Property

Public Property MinScale() As Integer
Get
Return mMinScale
End Get
Set(ByVal Value As Integer)
mMinScale = Value
End Set
End Property

Public Property MaxScale() As Integer
Get
Return mMaxScale
End Get
Set(ByVal Value As Integer)
mMaxScale = Value
End Set
End Property

Public ReadOnly Property UserDefined() As Boolean
Get
Return mUserDefined
End Get
End Property

Public Property Saved() As Boolean
Get
Return mSaved
End Get
Set(ByVal Value As Boolean)
mSaved = Value
End Set
End Property

Public Property Symbols() As CLayerSymbol()
Get
Return mSymbols
End Get
Set(ByVal Value As CLayerSymbol())
mSymbols = Value
End Set
End Property
#End Region

#Region "Public member methods"
Public Sub Dispose() Implements System.IDisposable.Dispose
CleanUp()
GC.SuppressFinalize(Me)
End Sub

Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
If TypeOf obj Is CLayerDefinition Then
Dim cld As CLayerDefinition = DirectCast(obj, CLayerDefinition)
mDrawOrder.CompareTo(cld.mDrawOrder)
End If

Throw New ArgumentException("obj is not a CLayerDefintion type")
End Function

#End Region

#Region "Private member methods"
Private Sub CleanUp()
If Not mSymbols Is Nothing Then
If mSymbols.GetUpperBound(0) >= 0 Then
Erase mSymbols
End If
mSymbols = Nothing
End If
End Sub
#End Region

#Region "Constructors"
Public Sub New()
mUserDefined = False
End Sub

Public Sub New(ByVal usrDefined As Boolean)
mUserDefined = usrDefined
End Sub
#End Region
End Class

Nov 12 '05 #2
Christoph,
Thanks for the link. I'm reading the document now and after looking at the
error message in more detail, it appears that my CLayerDefinition contains
another type CLayerSymbol, which contains a public property called Font
which is of type System.Font. The XmlSerializer is throwing an exception
when it tries to create this type because it does not have a default, public
constructor. Is there a way to help it do this without changing my
underlying class structure to some other type that has a default
constructor. Here's the class that it's having trouble with:

<Serializable()> _
Public Class CLayerSymbol

#Region "Private member simple objects"
Private mThemeID As Integer
Private mDescription As String
Private mFieldValue As Object
Private mStyle As String
Private mGlyphIndex As String
Private mColor As Integer
Private mSize As Integer
Private mOutlineColor As Integer
Private mOutline As Boolean
Private mFromColor As Integer
Private mToColor As Integer
Private mRotation As Integer
Private mDuplicateLabel As Boolean
Private mBold As Boolean
Private mItalic As Boolean
Private mUnderline As Boolean
Private mSpline As Boolean
Private mVerticalAlignment As Integer
Private mHorizontalAlignment As Integer
Private mFont As Font
#End Region

#Region "Properties"
Public Property font() As font
Get
Return mFont
End Get
Set(ByVal Value As font)
mFont = Value
End Set
End Property

Public Property VerticalAlignment() As Integer
Get
Return mVerticalAlignment
End Get
Set(ByVal Value As Integer)
mVerticalAlignment = Value
End Set
End Property

Public Property HorizontalAlignment() As Integer
Get
Return mHorizontalAlignment
End Get
Set(ByVal Value As Integer)
mHorizontalAlignment = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal Value As String)
mDescription = Value
End Set
End Property

<System.Xml.Serialization.XmlAnyElement("FieldValu e")> _
Public Property FieldValue() As Object
Get
Return mFieldValue
End Get
Set(ByVal Value As Object)
mFieldValue = Value
End Set
End Property

Public Property Style() As Integer
Get
Return mStyle
End Get
Set(ByVal Value As Integer)
mStyle = Value
End Set
End Property

Public Property GlyphIndex() As Integer
Get
Return mGlyphIndex
End Get
Set(ByVal Value As Integer)
mGlyphIndex = Value
End Set
End Property

Public Property Color() As Integer
Get
Return mColor
End Get
Set(ByVal Value As Integer)
mColor = Value
End Set
End Property

Public Property Size() As Integer
Get
Return mSize
End Get
Set(ByVal Value As Integer)
mSize = Value
End Set
End Property

Public Property OutlineColor() As Integer
Get
Return mOutlineColor
End Get
Set(ByVal Value As Integer)
mOutlineColor = Value
End Set
End Property

Public Property Outline() As Boolean
Get
Return mOutline
End Get
Set(ByVal Value As Boolean)
mOutline = Value
End Set
End Property

Public Property FromColor() As Integer
Get
Return mFromColor
End Get
Set(ByVal Value As Integer)
mFromColor = Value
End Set
End Property

Public Property ToColor() As Integer
Get
Return mToColor
End Get
Set(ByVal Value As Integer)
mToColor = Value
End Set
End Property

Public Property Rotation() As Integer
Get
Return mRotation
End Get
Set(ByVal Value As Integer)
mRotation = Value
End Set
End Property

Public Property DuplicateLabel() As Boolean
Get
Return mDuplicateLabel
End Get
Set(ByVal Value As Boolean)
mDuplicateLabel = Value
End Set
End Property

Public Property Bold() As Boolean
Get
Return mBold
End Get
Set(ByVal Value As Boolean)
mBold = Value
End Set
End Property

Public Property Italic() As Boolean
Get
Return mItalic
End Get
Set(ByVal Value As Boolean)
mItalic = Value
End Set
End Property

Public Property Underline() As Boolean
Get
Return mUnderline
End Get
Set(ByVal Value As Boolean)
mUnderline = Value
End Set
End Property

Public Property Spline() As Boolean
Get
Return mSpline
End Get
Set(ByVal Value As Boolean)
mSpline = Value
End Set
End Property
#End Region
End Class
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:uE**************@TK2MSFTNGP14.phx.gbl...

Steve,

Did you read through [0]?

It explains a number of scenarios that can cause this error.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0]
http://msdn.microsoft.com/XML/Buildi...k/default.aspx
?pull=/library/en-us/dnxmlnet/html/trblshtxsd.asp
-----Original Message-----
From: Steve Long [mailto:St**********@NoSpam.com]
Posted At: Tuesday, February 01, 2005 3:20 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer instantiation error
Subject: XmlSerializer instantiation error

Hello,
I hope this is the right group to post this to. I'm trying to

serialize a
class I've written and I'd like to be able to serialze to both binary

and
xml formats. Binary serialization is working fine but when I try to
instantiate an XmlSerializer object with:

Dim xmls As New XmlSerializer(GetType(CLayerDefinition))

I get the following error:

An unhandled exception of type 'System.InvalidOperationException'

occurred
in system.xml.dll

Additional information: There was an error reflecting type
'xmlSerializeTest.CLayerDefinition'.

Can anybody give me some pointer as to why this might be happening

since
the
error message is not too helpful?

Thanks Steve

The class CLayerDefinition is laid out as:

<Serializable()> _
Public Class CLayerDefinition
Implements IDisposable, IComparable

#Region "Private simple objects"
Private mThemeID As Integer
Private mLayerName As String
Private mThemeName As String
Private mLocation As String
Private mDraw As Boolean
Private mMinScale As Integer
Private mMaxScale As Integer
Private mUserDefined As Boolean
Private mSaved As Boolean
Private mDrawOrder As Integer
Private mLegendType As Integer
Private mSymbolType As Integer
Private mLabelField As String
Private mHasImages As Boolean
Private mRelateTable As String
Private mTableLocation As String
Private mRelateField As String
Private mFileNameField As String
Private mClassItemField As String
Private mNumberOfBreaks As Integer
#End Region

#Region "Private complex objects"
<XmlArray()> _
Private mSymbols() As CLayerSymbol
#End Region

#Region "Public properties"
Public Property NumberOfBreaks() As Integer
Get
Return mNumberOfBreaks
End Get
Set(ByVal Value As Integer)
mNumberOfBreaks = Value
End Set
End Property

Public Property ClassItemField() As String
Get
Return mClassItemField
End Get
Set(ByVal Value As String)
mClassItemField = Value
End Set
End Property

Public Property HasImages() As Boolean
Get
Return mHasImages
End Get
Set(ByVal Value As Boolean)
mHasImages = Value
End Set
End Property

Public Property FileNameField() As String
Get
Return mFileNameField
End Get
Set(ByVal Value As String)
mFileNameField = Value
End Set
End Property

Public Property RelateField() As String
Get
Return mRelateField
End Get
Set(ByVal Value As String)
mRelateField = Value
End Set
End Property

Public Property TableLocation() As String
Get
Return mTableLocation
End Get
Set(ByVal Value As String)
mTableLocation = Value
End Set
End Property

Public Property RelateTable() As String
Get
Return mRelateTable
End Get
Set(ByVal Value As String)
mRelateTable = Value
End Set
End Property

Public Property LabelField() As String
Get
Return mLabelField
End Get
Set(ByVal Value As String)
mLabelField = Value
End Set
End Property

Public Property SymbolType() As Integer
Get
Return mSymbolType
End Get
Set(ByVal Value As Integer)
mSymbolType = Value
End Set
End Property

Public Property LegendType() As Integer
Get
Return mLegendType
End Get
Set(ByVal Value As Integer)
mLegendType = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property LayerName() As String
Get
Return mLayerName
End Get
Set(ByVal Value As String)
mLayerName = Value
End Set
End Property

Public Property ThemeName() As String
Get
Return mThemeName
End Get
Set(ByVal Value As String)
mThemeName = Value
End Set
End Property

Public Property Location() As String
Get
Return mLocation
End Get
Set(ByVal Value As String)
mLocation = Value
End Set
End Property

Public Property DrawOrder() As Integer
Get
Return mDrawOrder
End Get
Set(ByVal Value As Integer)
mDrawOrder = Value
End Set
End Property

Public Property Draw() As Boolean
Get
Return mDraw
End Get
Set(ByVal Value As Boolean)
mDraw = Value
End Set
End Property

Public Property MinScale() As Integer
Get
Return mMinScale
End Get
Set(ByVal Value As Integer)
mMinScale = Value
End Set
End Property

Public Property MaxScale() As Integer
Get
Return mMaxScale
End Get
Set(ByVal Value As Integer)
mMaxScale = Value
End Set
End Property

Public ReadOnly Property UserDefined() As Boolean
Get
Return mUserDefined
End Get
End Property

Public Property Saved() As Boolean
Get
Return mSaved
End Get
Set(ByVal Value As Boolean)
mSaved = Value
End Set
End Property

Public Property Symbols() As CLayerSymbol()
Get
Return mSymbols
End Get
Set(ByVal Value As CLayerSymbol())
mSymbols = Value
End Set
End Property
#End Region

#Region "Public member methods"
Public Sub Dispose() Implements System.IDisposable.Dispose
CleanUp()
GC.SuppressFinalize(Me)
End Sub

Public Function CompareTo(ByVal obj As Object) As Integer

Implements
System.IComparable.CompareTo
If TypeOf obj Is CLayerDefinition Then
Dim cld As CLayerDefinition = DirectCast(obj,

CLayerDefinition)

mDrawOrder.CompareTo(cld.mDrawOrder)
End If

Throw New ArgumentException("obj is not a CLayerDefintion type")
End Function

#End Region

#Region "Private member methods"
Private Sub CleanUp()
If Not mSymbols Is Nothing Then
If mSymbols.GetUpperBound(0) >= 0 Then
Erase mSymbols
End If
mSymbols = Nothing
End If
End Sub
#End Region

#Region "Constructors"
Public Sub New()
mUserDefined = False
End Sub

Public Sub New(ByVal usrDefined As Boolean)
mUserDefined = usrDefined
End Sub
#End Region
End Class


Nov 12 '05 #3
Steve,

What you're describing is pretty much the best option. Wrap the Font
class in another class that has a default constructor and exposes
properties for the data you need to serialize plus a read/write property
to store the Font object. Then decorate that Font property with the
XmlIgnore attribute to tell the XmlSerializer to skip it.

Unfortunately, there isn't anything more sophisticated. The runtime
formatters (BinaryFormatter, SoapFormatter) provide much better hooks
into serialization and deserialization than the XmlSerializer.
HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko
-----Original Message-----
From: Steve Long [mailto:St**********@NoSpam.com]
Posted At: Wednesday, February 02, 2005 10:36 AM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer instantiation error
Subject: Re: XmlSerializer instantiation error

Christoph,
Thanks for the link. I'm reading the document now and after looking at the error message in more detail, it appears that my CLayerDefinition contains another type CLayerSymbol, which contains a public property called Font which is of type System.Font. The XmlSerializer is throwing an exception when it tries to create this type because it does not have a default,
public
constructor. Is there a way to help it do this without changing my
underlying class structure to some other type that has a default
constructor. Here's the class that it's having trouble with:

<Serializable()> _
Public Class CLayerSymbol

#Region "Private member simple objects"
Private mThemeID As Integer
Private mDescription As String
Private mFieldValue As Object
Private mStyle As String
Private mGlyphIndex As String
Private mColor As Integer
Private mSize As Integer
Private mOutlineColor As Integer
Private mOutline As Boolean
Private mFromColor As Integer
Private mToColor As Integer
Private mRotation As Integer
Private mDuplicateLabel As Boolean
Private mBold As Boolean
Private mItalic As Boolean
Private mUnderline As Boolean
Private mSpline As Boolean
Private mVerticalAlignment As Integer
Private mHorizontalAlignment As Integer
Private mFont As Font
#End Region

#Region "Properties"
Public Property font() As font
Get
Return mFont
End Get
Set(ByVal Value As font)
mFont = Value
End Set
End Property

Public Property VerticalAlignment() As Integer
Get
Return mVerticalAlignment
End Get
Set(ByVal Value As Integer)
mVerticalAlignment = Value
End Set
End Property

Public Property HorizontalAlignment() As Integer
Get
Return mHorizontalAlignment
End Get
Set(ByVal Value As Integer)
mHorizontalAlignment = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal Value As String)
mDescription = Value
End Set
End Property

<System.Xml.Serialization.XmlAnyElement("FieldValu e")> _
Public Property FieldValue() As Object
Get
Return mFieldValue
End Get
Set(ByVal Value As Object)
mFieldValue = Value
End Set
End Property

Public Property Style() As Integer
Get
Return mStyle
End Get
Set(ByVal Value As Integer)
mStyle = Value
End Set
End Property

Public Property GlyphIndex() As Integer
Get
Return mGlyphIndex
End Get
Set(ByVal Value As Integer)
mGlyphIndex = Value
End Set
End Property

Public Property Color() As Integer
Get
Return mColor
End Get
Set(ByVal Value As Integer)
mColor = Value
End Set
End Property

Public Property Size() As Integer
Get
Return mSize
End Get
Set(ByVal Value As Integer)
mSize = Value
End Set
End Property

Public Property OutlineColor() As Integer
Get
Return mOutlineColor
End Get
Set(ByVal Value As Integer)
mOutlineColor = Value
End Set
End Property

Public Property Outline() As Boolean
Get
Return mOutline
End Get
Set(ByVal Value As Boolean)
mOutline = Value
End Set
End Property

Public Property FromColor() As Integer
Get
Return mFromColor
End Get
Set(ByVal Value As Integer)
mFromColor = Value
End Set
End Property

Public Property ToColor() As Integer
Get
Return mToColor
End Get
Set(ByVal Value As Integer)
mToColor = Value
End Set
End Property

Public Property Rotation() As Integer
Get
Return mRotation
End Get
Set(ByVal Value As Integer)
mRotation = Value
End Set
End Property

Public Property DuplicateLabel() As Boolean
Get
Return mDuplicateLabel
End Get
Set(ByVal Value As Boolean)
mDuplicateLabel = Value
End Set
End Property

Public Property Bold() As Boolean
Get
Return mBold
End Get
Set(ByVal Value As Boolean)
mBold = Value
End Set
End Property

Public Property Italic() As Boolean
Get
Return mItalic
End Get
Set(ByVal Value As Boolean)
mItalic = Value
End Set
End Property

Public Property Underline() As Boolean
Get
Return mUnderline
End Get
Set(ByVal Value As Boolean)
mUnderline = Value
End Set
End Property

Public Property Spline() As Boolean
Get
Return mSpline
End Get
Set(ByVal Value As Boolean)
mSpline = Value
End Set
End Property
#End Region
End Class
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message news:uE**************@TK2MSFTNGP14.phx.gbl...

Steve,

Did you read through [0]?

It explains a number of scenarios that can cause this error.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0]
http://msdn.microsoft.com/XML/Buildi...k/default.aspx ?pull=/library/en-us/dnxmlnet/html/trblshtxsd.asp
-----Original Message-----
From: Steve Long [mailto:St**********@NoSpam.com]
Posted At: Tuesday, February 01, 2005 3:20 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer instantiation error
Subject: XmlSerializer instantiation error

Hello,
I hope this is the right group to post this to. I'm trying to

serialize a
class I've written and I'd like to be able to serialze to both binary
and
xml formats. Binary serialization is working fine but when I try

to instantiate an XmlSerializer object with:

Dim xmls As New XmlSerializer(GetType(CLayerDefinition))

I get the following error:

An unhandled exception of type 'System.InvalidOperationException'

occurred
in system.xml.dll

Additional information: There was an error reflecting type
'xmlSerializeTest.CLayerDefinition'.

Can anybody give me some pointer as to why this might be happening

since
the
error message is not too helpful?

Thanks Steve

The class CLayerDefinition is laid out as:

<Serializable()> _
Public Class CLayerDefinition
Implements IDisposable, IComparable

#Region "Private simple objects"
Private mThemeID As Integer
Private mLayerName As String
Private mThemeName As String
Private mLocation As String
Private mDraw As Boolean
Private mMinScale As Integer
Private mMaxScale As Integer
Private mUserDefined As Boolean
Private mSaved As Boolean
Private mDrawOrder As Integer
Private mLegendType As Integer
Private mSymbolType As Integer
Private mLabelField As String
Private mHasImages As Boolean
Private mRelateTable As String
Private mTableLocation As String
Private mRelateField As String
Private mFileNameField As String
Private mClassItemField As String
Private mNumberOfBreaks As Integer
#End Region

#Region "Private complex objects"
<XmlArray()> _
Private mSymbols() As CLayerSymbol
#End Region

#Region "Public properties"
Public Property NumberOfBreaks() As Integer
Get
Return mNumberOfBreaks
End Get
Set(ByVal Value As Integer)
mNumberOfBreaks = Value
End Set
End Property

Public Property ClassItemField() As String
Get
Return mClassItemField
End Get
Set(ByVal Value As String)
mClassItemField = Value
End Set
End Property

Public Property HasImages() As Boolean
Get
Return mHasImages
End Get
Set(ByVal Value As Boolean)
mHasImages = Value
End Set
End Property

Public Property FileNameField() As String
Get
Return mFileNameField
End Get
Set(ByVal Value As String)
mFileNameField = Value
End Set
End Property

Public Property RelateField() As String
Get
Return mRelateField
End Get
Set(ByVal Value As String)
mRelateField = Value
End Set
End Property

Public Property TableLocation() As String
Get
Return mTableLocation
End Get
Set(ByVal Value As String)
mTableLocation = Value
End Set
End Property

Public Property RelateTable() As String
Get
Return mRelateTable
End Get
Set(ByVal Value As String)
mRelateTable = Value
End Set
End Property

Public Property LabelField() As String
Get
Return mLabelField
End Get
Set(ByVal Value As String)
mLabelField = Value
End Set
End Property

Public Property SymbolType() As Integer
Get
Return mSymbolType
End Get
Set(ByVal Value As Integer)
mSymbolType = Value
End Set
End Property

Public Property LegendType() As Integer
Get
Return mLegendType
End Get
Set(ByVal Value As Integer)
mLegendType = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property LayerName() As String
Get
Return mLayerName
End Get
Set(ByVal Value As String)
mLayerName = Value
End Set
End Property

Public Property ThemeName() As String
Get
Return mThemeName
End Get
Set(ByVal Value As String)
mThemeName = Value
End Set
End Property

Public Property Location() As String
Get
Return mLocation
End Get
Set(ByVal Value As String)
mLocation = Value
End Set
End Property

Public Property DrawOrder() As Integer
Get
Return mDrawOrder
End Get
Set(ByVal Value As Integer)
mDrawOrder = Value
End Set
End Property

Public Property Draw() As Boolean
Get
Return mDraw
End Get
Set(ByVal Value As Boolean)
mDraw = Value
End Set
End Property

Public Property MinScale() As Integer
Get
Return mMinScale
End Get
Set(ByVal Value As Integer)
mMinScale = Value
End Set
End Property

Public Property MaxScale() As Integer
Get
Return mMaxScale
End Get
Set(ByVal Value As Integer)
mMaxScale = Value
End Set
End Property

Public ReadOnly Property UserDefined() As Boolean
Get
Return mUserDefined
End Get
End Property

Public Property Saved() As Boolean
Get
Return mSaved
End Get
Set(ByVal Value As Boolean)
mSaved = Value
End Set
End Property

Public Property Symbols() As CLayerSymbol()
Get
Return mSymbols
End Get
Set(ByVal Value As CLayerSymbol())
mSymbols = Value
End Set
End Property
#End Region

#Region "Public member methods"
Public Sub Dispose() Implements System.IDisposable.Dispose
CleanUp()
GC.SuppressFinalize(Me)
End Sub

Public Function CompareTo(ByVal obj As Object) As Integer

Implements
System.IComparable.CompareTo
If TypeOf obj Is CLayerDefinition Then
Dim cld As CLayerDefinition = DirectCast(obj,

CLayerDefinition)

mDrawOrder.CompareTo(cld.mDrawOrder)
End If

Throw New ArgumentException("obj is not a CLayerDefintion type") End Function

#End Region

#Region "Private member methods"
Private Sub CleanUp()
If Not mSymbols Is Nothing Then
If mSymbols.GetUpperBound(0) >= 0 Then
Erase mSymbols
End If
mSymbols = Nothing
End If
End Sub
#End Region

#Region "Constructors"
Public Sub New()
mUserDefined = False
End Sub

Public Sub New(ByVal usrDefined As Boolean)
mUserDefined = usrDefined
End Sub
#End Region
End Class


Nov 12 '05 #4
Thanks Christoph. What I decided to do was to create my own CFont class that
exposes the properties that I need that includes a default constructor.

Steve
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Steve,

What you're describing is pretty much the best option. Wrap the Font
class in another class that has a default constructor and exposes
properties for the data you need to serialize plus a read/write property
to store the Font object. Then decorate that Font property with the
XmlIgnore attribute to tell the XmlSerializer to skip it.

Unfortunately, there isn't anything more sophisticated. The runtime
formatters (BinaryFormatter, SoapFormatter) provide much better hooks
into serialization and deserialization than the XmlSerializer.
HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko
-----Original Message-----
From: Steve Long [mailto:St**********@NoSpam.com]
Posted At: Wednesday, February 02, 2005 10:36 AM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer instantiation error
Subject: Re: XmlSerializer instantiation error

Christoph,
Thanks for the link. I'm reading the document now and after looking at

the
error message in more detail, it appears that my CLayerDefinition

contains
another type CLayerSymbol, which contains a public property called

Font
which is of type System.Font. The XmlSerializer is throwing an

exception
when it tries to create this type because it does not have a default,
public
constructor. Is there a way to help it do this without changing my
underlying class structure to some other type that has a default
constructor. Here's the class that it's having trouble with:

<Serializable()> _
Public Class CLayerSymbol

#Region "Private member simple objects"
Private mThemeID As Integer
Private mDescription As String
Private mFieldValue As Object
Private mStyle As String
Private mGlyphIndex As String
Private mColor As Integer
Private mSize As Integer
Private mOutlineColor As Integer
Private mOutline As Boolean
Private mFromColor As Integer
Private mToColor As Integer
Private mRotation As Integer
Private mDuplicateLabel As Boolean
Private mBold As Boolean
Private mItalic As Boolean
Private mUnderline As Boolean
Private mSpline As Boolean
Private mVerticalAlignment As Integer
Private mHorizontalAlignment As Integer
Private mFont As Font
#End Region

#Region "Properties"
Public Property font() As font
Get
Return mFont
End Get
Set(ByVal Value As font)
mFont = Value
End Set
End Property

Public Property VerticalAlignment() As Integer
Get
Return mVerticalAlignment
End Get
Set(ByVal Value As Integer)
mVerticalAlignment = Value
End Set
End Property

Public Property HorizontalAlignment() As Integer
Get
Return mHorizontalAlignment
End Get
Set(ByVal Value As Integer)
mHorizontalAlignment = Value
End Set
End Property

Public Property ThemeID() As Integer
Get
Return mThemeID
End Get
Set(ByVal Value As Integer)
mThemeID = Value
End Set
End Property

Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal Value As String)
mDescription = Value
End Set
End Property

<System.Xml.Serialization.XmlAnyElement("FieldValu e")> _
Public Property FieldValue() As Object
Get
Return mFieldValue
End Get
Set(ByVal Value As Object)
mFieldValue = Value
End Set
End Property

Public Property Style() As Integer
Get
Return mStyle
End Get
Set(ByVal Value As Integer)
mStyle = Value
End Set
End Property

Public Property GlyphIndex() As Integer
Get
Return mGlyphIndex
End Get
Set(ByVal Value As Integer)
mGlyphIndex = Value
End Set
End Property

Public Property Color() As Integer
Get
Return mColor
End Get
Set(ByVal Value As Integer)
mColor = Value
End Set
End Property

Public Property Size() As Integer
Get
Return mSize
End Get
Set(ByVal Value As Integer)
mSize = Value
End Set
End Property

Public Property OutlineColor() As Integer
Get
Return mOutlineColor
End Get
Set(ByVal Value As Integer)
mOutlineColor = Value
End Set
End Property

Public Property Outline() As Boolean
Get
Return mOutline
End Get
Set(ByVal Value As Boolean)
mOutline = Value
End Set
End Property

Public Property FromColor() As Integer
Get
Return mFromColor
End Get
Set(ByVal Value As Integer)
mFromColor = Value
End Set
End Property

Public Property ToColor() As Integer
Get
Return mToColor
End Get
Set(ByVal Value As Integer)
mToColor = Value
End Set
End Property

Public Property Rotation() As Integer
Get
Return mRotation
End Get
Set(ByVal Value As Integer)
mRotation = Value
End Set
End Property

Public Property DuplicateLabel() As Boolean
Get
Return mDuplicateLabel
End Get
Set(ByVal Value As Boolean)
mDuplicateLabel = Value
End Set
End Property

Public Property Bold() As Boolean
Get
Return mBold
End Get
Set(ByVal Value As Boolean)
mBold = Value
End Set
End Property

Public Property Italic() As Boolean
Get
Return mItalic
End Get
Set(ByVal Value As Boolean)
mItalic = Value
End Set
End Property

Public Property Underline() As Boolean
Get
Return mUnderline
End Get
Set(ByVal Value As Boolean)
mUnderline = Value
End Set
End Property

Public Property Spline() As Boolean
Get
Return mSpline
End Get
Set(ByVal Value As Boolean)
mSpline = Value
End Set
End Property
#End Region
End Class
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in

message
news:uE**************@TK2MSFTNGP14.phx.gbl...

Steve,

Did you read through [0]?

It explains a number of scenarios that can cause this error.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0]
http://msdn.microsoft.com/XML/Buildi...k/default.aspx ?pull=/library/en-us/dnxmlnet/html/trblshtxsd.asp

> -----Original Message-----
> From: Steve Long [mailto:St**********@NoSpam.com]
> Posted At: Tuesday, February 01, 2005 3:20 PM
> Posted To: microsoft.public.dotnet.xml
> Conversation: XmlSerializer instantiation error
> Subject: XmlSerializer instantiation error
>
> Hello,
> I hope this is the right group to post this to. I'm trying to
serialize a
> class I've written and I'd like to be able to serialze to both binary and
> xml formats. Binary serialization is working fine but when I try to > instantiate an XmlSerializer object with:
>
> Dim xmls As New XmlSerializer(GetType(CLayerDefinition))
>
> I get the following error:
>
> An unhandled exception of type 'System.InvalidOperationException'
occurred
> in system.xml.dll
>
> Additional information: There was an error reflecting type
> 'xmlSerializeTest.CLayerDefinition'.
>
> Can anybody give me some pointer as to why this might be happening
since
> the
> error message is not too helpful?
>
> Thanks Steve
>
> The class CLayerDefinition is laid out as:
>
> <Serializable()> _
> Public Class CLayerDefinition
> Implements IDisposable, IComparable
>
> #Region "Private simple objects"
> Private mThemeID As Integer
> Private mLayerName As String
> Private mThemeName As String
> Private mLocation As String
> Private mDraw As Boolean
> Private mMinScale As Integer
> Private mMaxScale As Integer
> Private mUserDefined As Boolean
> Private mSaved As Boolean
> Private mDrawOrder As Integer
> Private mLegendType As Integer
> Private mSymbolType As Integer
> Private mLabelField As String
> Private mHasImages As Boolean
> Private mRelateTable As String
> Private mTableLocation As String
> Private mRelateField As String
> Private mFileNameField As String
> Private mClassItemField As String
> Private mNumberOfBreaks As Integer
> #End Region
>
> #Region "Private complex objects"
> <XmlArray()> _
> Private mSymbols() As CLayerSymbol
> #End Region
>
> #Region "Public properties"
> Public Property NumberOfBreaks() As Integer
> Get
> Return mNumberOfBreaks
> End Get
> Set(ByVal Value As Integer)
> mNumberOfBreaks = Value
> End Set
> End Property
>
> Public Property ClassItemField() As String
> Get
> Return mClassItemField
> End Get
> Set(ByVal Value As String)
> mClassItemField = Value
> End Set
> End Property
>
> Public Property HasImages() As Boolean
> Get
> Return mHasImages
> End Get
> Set(ByVal Value As Boolean)
> mHasImages = Value
> End Set
> End Property
>
> Public Property FileNameField() As String
> Get
> Return mFileNameField
> End Get
> Set(ByVal Value As String)
> mFileNameField = Value
> End Set
> End Property
>
> Public Property RelateField() As String
> Get
> Return mRelateField
> End Get
> Set(ByVal Value As String)
> mRelateField = Value
> End Set
> End Property
>
> Public Property TableLocation() As String
> Get
> Return mTableLocation
> End Get
> Set(ByVal Value As String)
> mTableLocation = Value
> End Set
> End Property
>
> Public Property RelateTable() As String
> Get
> Return mRelateTable
> End Get
> Set(ByVal Value As String)
> mRelateTable = Value
> End Set
> End Property
>
> Public Property LabelField() As String
> Get
> Return mLabelField
> End Get
> Set(ByVal Value As String)
> mLabelField = Value
> End Set
> End Property
>
> Public Property SymbolType() As Integer
> Get
> Return mSymbolType
> End Get
> Set(ByVal Value As Integer)
> mSymbolType = Value
> End Set
> End Property
>
> Public Property LegendType() As Integer
> Get
> Return mLegendType
> End Get
> Set(ByVal Value As Integer)
> mLegendType = Value
> End Set
> End Property
>
> Public Property ThemeID() As Integer
> Get
> Return mThemeID
> End Get
> Set(ByVal Value As Integer)
> mThemeID = Value
> End Set
> End Property
>
> Public Property LayerName() As String
> Get
> Return mLayerName
> End Get
> Set(ByVal Value As String)
> mLayerName = Value
> End Set
> End Property
>
> Public Property ThemeName() As String
> Get
> Return mThemeName
> End Get
> Set(ByVal Value As String)
> mThemeName = Value
> End Set
> End Property
>
> Public Property Location() As String
> Get
> Return mLocation
> End Get
> Set(ByVal Value As String)
> mLocation = Value
> End Set
> End Property
>
> Public Property DrawOrder() As Integer
> Get
> Return mDrawOrder
> End Get
> Set(ByVal Value As Integer)
> mDrawOrder = Value
> End Set
> End Property
>
> Public Property Draw() As Boolean
> Get
> Return mDraw
> End Get
> Set(ByVal Value As Boolean)
> mDraw = Value
> End Set
> End Property
>
> Public Property MinScale() As Integer
> Get
> Return mMinScale
> End Get
> Set(ByVal Value As Integer)
> mMinScale = Value
> End Set
> End Property
>
> Public Property MaxScale() As Integer
> Get
> Return mMaxScale
> End Get
> Set(ByVal Value As Integer)
> mMaxScale = Value
> End Set
> End Property
>
> Public ReadOnly Property UserDefined() As Boolean
> Get
> Return mUserDefined
> End Get
> End Property
>
> Public Property Saved() As Boolean
> Get
> Return mSaved
> End Get
> Set(ByVal Value As Boolean)
> mSaved = Value
> End Set
> End Property
>
> Public Property Symbols() As CLayerSymbol()
> Get
> Return mSymbols
> End Get
> Set(ByVal Value As CLayerSymbol())
> mSymbols = Value
> End Set
> End Property
> #End Region
>
> #Region "Public member methods"
> Public Sub Dispose() Implements System.IDisposable.Dispose
> CleanUp()
> GC.SuppressFinalize(Me)
> End Sub
>
> Public Function CompareTo(ByVal obj As Object) As Integer
Implements
> System.IComparable.CompareTo
> If TypeOf obj Is CLayerDefinition Then
> Dim cld As CLayerDefinition = DirectCast(obj,
CLayerDefinition)
>
> mDrawOrder.CompareTo(cld.mDrawOrder)
> End If
>
> Throw New ArgumentException("obj is not a CLayerDefintion type") > End Function
>
> #End Region
>
> #Region "Private member methods"
> Private Sub CleanUp()
> If Not mSymbols Is Nothing Then
> If mSymbols.GetUpperBound(0) >= 0 Then
> Erase mSymbols
> End If
> mSymbols = Nothing
> End If
> End Sub
> #End Region
>
> #Region "Constructors"
> Public Sub New()
> mUserDefined = False
> End Sub
>
> Public Sub New(ByVal usrDefined As Boolean)
> mUserDefined = usrDefined
> End Sub
> #End Region
> End Class


Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Harris Boyce | last post: by
2 posts views Thread by Chris Aitchison | last post: by
11 posts views Thread by Rangi Keen | last post: by
reply views Thread by William Stacey [MVP] | last post: by
reply views Thread by awood45 | last post: by
reply views Thread by leo001 | last post: by

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.