473,561 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ 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(Seria lization info,StreamingC ontext context) :
base(info,conte xt)
{
}
}

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

XmlSerializer ser = new XmlSerializer(t ypeof(DataSet)) ;

StringWriter writer = new StringWriter();

ser.Serialize(w riter, ds);

........

........

// desirializing the class

MyDataSet newDs = new MyDataSet();

StreamReader reader = new StreamReader("S er.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 2346
Hi,

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

Try to override ISerializable.G etObjectData(.. .), and don't forget
to call ((ISerializable ) base).GetObject Data(...).

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(Seria lization info,StreamingC ontext context) :
base(info,conte xt)
{
}
}

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

XmlSerializer ser = new XmlSerializer(t ypeof(DataSet)) ;

StringWriter writer = new StringWriter();

ser.Serialize(w riter, ds);

.......

.......

// desirializing the class

MyDataSet newDs = new MyDataSet();

StreamReader reader = new StreamReader("S er.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.
XmlSerializatio n 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.co m

"ofer" <pe******@bguma il.bgu.ac.il> wrote in message
news:%2******** ********@TK2MSF TNGP09.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(Seria lization info,StreamingC ontext context) :
base(info,conte xt)
{
}
}

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

XmlSerializer ser = new XmlSerializer(t ypeof(DataSet)) ;

StringWriter writer = new StringWriter();

ser.Serialize(w riter, ds);

.......

.......

// desirializing the class

MyDataSet newDs = new MyDataSet();

StreamReader reader = new StreamReader("S er.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
1487
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 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...
0
1477
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 c:\LibSerial\LibSerial.cpp(83) : warning C4584: 'serial::E' : base-class 'serial::A' is already a base-class of 'serial::C' c:\LibSerial\LibSerial.cpp(10) : see...
4
8697
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 ds2 = (DataSet)formatter2.Deserialize(stream2); For the size of my DataSet, its taking 0.8 seconds to serialize and 2.3 seconds to deserialize.
3
3161
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. It seems that either I can use default serialization or implement ISerializable. Is there any way to do both (e.g. extend the default...
3
394
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 file, I want an XML-object (XmlNode) instead that I can pass as a parameter to another method. How can I do that?
4
2199
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 both objects have a non-static member reference to some other static object. In this case, I get an InvalidArgument exception when I try to access...
2
5070
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 a number of other classes, I was assuming that I could just mark them as and use automatic serialization but this does not work (The new class...
7
1786
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 which you can see I'm using when calling AddValue. I implemented ISerializable and GeteObjectData. Here's what I tried: for (int i = 0; i <...
1
2200
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 networks, not due to SOAP schemas. I think that's because a XML Dataset is too large. So, in my WS, I want to turn my dataset in some type of...
0
7644
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7928
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6206
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2066
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 we have to send another system
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.