By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

serialization problem with a class that derives from dataset

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
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.

Nov 16 '05 #2

P: n/a
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ofer" <pe******@bgumail.bgu.ac.il> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.