serialization problem with a class that derives from dataset

O

ofer

Hi,

I am working with the beta version of the new .net framework (Whidbey) and I
encountered a problem with serialization that did'nt exist in the .net 2003
the situation is like this :
I have a class that inherits from dataset, and I want to serialize it , so I
created a serialization constructor that forwards the call to the base class
(the dataset) serialization constructor, normally, for this action to
succeed I am supposed to put an attribute on the super class (in this case
the dataset) that looks like that :
[XmlInclude (typeOf (<myClass>))]

so that the serializer will be able to deserialize my class where a dataset
was daclared, the problem is, that I can't put this attribute on the
dataset.
currrently the serializer is able to serialize my class but when it
desirializes it I get a dataset object and I can't cast it to the derived
class (casting fails),
so I need an alternative way to let the serializer know about the derived
class, so if anyone knows a way to do this without using the attribute I
would appreciate his help.

code sample that demonstrates the situation:

// the derived class

[Serializeable]
public class MyDataSet : DataSet
{
public MyDataSet() : base()
{
}

// serialization constructor
public MyDataSet(Serialization info,StreamingContext context) :
base(info,context)
{
}
}

// serializing the class
......
.......
MyDataSet ds = new MyDataSet();

XmlSerializer ser = new XmlSerializer(typeof(DataSet));

StringWriter writer = new StringWriter();

ser.Serialize(writer, ds);

........

........

// desirializing the class

MyDataSet newDs = new MyDataSet();

StreamReader reader = new StreamReader("Ser.Xml", Encoding.UTF8);

newDs = ser.Deserialize(reader) as MyDataSet; // at this point the casting
fails and returns null.

.......

.......



thanks , ofer.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi,

I can only point you the solution, but i'm not sure how it works...

Try to override ISerializable.GetObjectData(...), and don't forget
to call ((ISerializable) base).GetObjectData(...).

Maybe you should try other serializer (e.g. binary or SOAP) for
test reason?

Regards

Marcin
 
N

Nicholas Paldino [.NET/C# MVP]

ofer,

You are confusing the two different methods of serialization.
XmlSerialization does not use the Serializable attribute, or the custom
serialization constructor. This is for serialization using formatters that
implement the IFormatter interface.

If you need your class serialized into XML, assuming you don't have any
state beyond what is stored on the DataSet level, why not just call WriteXml
to get XML? Also, you might want to check out the BinaryFormatter or the
SoapFormatter.

Must your dataset be serialized into XML? If so, must that XML be
readable, or can it be incredibly complex?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top