472,143 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

serialization problem with a class that derives from dataset

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
2 2264
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
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.

Similar topics

reply views Thread by okinrus | last post: by
3 posts views Thread by Aaron Clamage | last post: by
3 posts views Thread by Janne | last post: by
4 posts views Thread by Aaron Clamage | last post: by
2 posts views Thread by tony lock | last post: by
7 posts views Thread by Joe | last post: by
1 post views Thread by Paez | 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.