XmlSerializationWriter - Specified cast is not valid

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

Code
public class RootSerialized
{
[XmlElement("SomeCollection")]
public SomeCollection SomeThings;
}

public class SomeCollection: BaseCollection ,ITypedList ,IBindingList
{
public SomeCollection() : base()
{
}
[XmlElement("Something")]
public Something this[int index]
{
get
{
return (Something )m_al[index];
}
}
}

During the serialization

XmlSerializer s = new XmlSerializer( typeof( RootSerialized ) );

this this structure return error:
System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidCastException: Specified cast is not valid.

So what is the problem?
 
Hi Tamir,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when serializing a class, there is an
System.InvalidOperationException, which says "There was an error generating
the XML document." is generated. If there is any misunderstanding, please
feel free to let me know.

Based on my research, this exception is thrown, because the BaseCollection
class is not serializable. This class does not implement the ISerializable
interface and it contains some complex data types. So when the object is
going to be serialized, the Serialize method doesn't know how to convert
data in the object, and the exception is thrown.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
So how can I serialize such structure?

Implement Iserializable.

(BTW, if you're going to top post, please don't use the sig dash - '--
' - as a lot of newsreaders will cut off quoting text after it, just like
this answer does.)
 
Hi Tamir,

To serialize such object, we have to implement ISerialization interface so
that the serializer will know how to serialize this object.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank you for your reply. The issue solved by workaround
Are there support for this at FW2.0?
 
Hi Tamir,

Since .NET framwork 2.0 hasn't been officially release, we are not quite
sure about the feature of it, because everything might be changed before
release.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top