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.
 

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