473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2332
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ofer | last post by:
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...
0
by: okinrus | last post by:
Can someone take a look at this code and figure out why Serializable_base::add_serializer throws std::bad_alloc. The problem seems to be the compiler because msvc++ 7.1 says...
4
by: hs | last post by:
Hi I am serializing a dataset using a binary formatter as follows: IFormatter formater = new BinaryFormatter(); formatter.Serialize(stream, ds); // ds=DataSet, stream=MemoryStream .... DataSet...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
3
by: Janne | last post by:
I have a problem with Xml and Serialization. I use serialization to get Xml as a result. All Serialization examples in Microsoft documentation writes the result to a file. I don't want a...
4
by: Aaron Clamage | last post by:
Hi, Could someone please confirm the following? I think I have found a subtle .NET serialization bug. It occurs when object has a list of items containing another object of the same type and...
2
by: tony lock | last post by:
I have a class inherited from Control, which I want to serialize, since Control is not Serializable, I have had to implement ISerializable. This works but I now want to inherit this base class into...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
1
by: Paez | last post by:
Hi There. This post is a extension of thread "Serialize or not to Serialize", on 5 of December of 2006. The reason why my teacher insists that a WS cannot return a Dataset is due to low rate...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.