Serialization Problem: custom properties of a derived DataSet class not serialized

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I created a strongly typed dataset using the designer and added a couple of
public properties via the Partial Class. Everything is fine and the
strongly typed data set functions as expected including the public
properties until the serialization/deserialization process. When all the
custom public properties are total ignored and lost. Is this behaviour by
design or simply a bug?

Thanks,

Michael
 
Hi Michael,

Are you using DataSet.WriteXml to serialize the DataSet object? The
DataSet.WriteXml will only write things that defined in DataSet schema
(.xsd file) to the Xml. It is by design.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Also, you can override the WriteXml method in your partial class to get
custom properties serialized.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank, Kevin

It sounds like that I have to overwrite the Dataset.WriteXml to include the
custom properties in the serialization. Do you have a code snippt for this?
I guess that I have to do call the base class WriteXml to serialize
everything specified by the dataset schema, as well as the custom
properties, right?

Michael
 
Kevin,
Are you using DataSet.WriteXml to serialize the DataSet object? The
DataSet.WriteXml will only write things that defined in DataSet schema
(.xsd file) to the Xml. It is by design.

Is that new, I almost never use a xsd file and do only

ds.WriteXml(path,parameter)

Cor
 
Michael,

Are your custom properties part of the dataset or are they outside the
dataset.

In otherwords is your property a column or is it just a property of your
class that you made with it.

AFAIK is a dataset forever a dataset, not a serialized object.

Cor
 
Kevin,

Thinking it over I get what you mean and we agree.
Are you using DataSet.WriteXml to serialize the DataSet object? The
DataSet.WriteXml will only write things that defined in DataSet schema
(.xsd file) to the Xml. It is by design.
Only you would have better written (by instance a .xsd file) in my opinion.
The schema can as well be a part of the XML file itself.

Cor
 
The property is unrelated to the dataset but defined as part of the class.
What it does not make any sense to me is that the class is marked as
serializable but the public property is not serialized. This seems
contradict to basic rules of serialization.
 
Thanks for the reply.

Neither WriteXml nor normal serialization as the code suggested by your link
does the job. I'm bit frustrated as you and Kevin seemed to know the
anwser, but yet have provided any thing with clearity on this.

Michael
 
Hi Micheal,

The custom properties are actually a party of the DataSet schema. If you
only write the Xml data, the custom properties will not be persisted. So in
this case if you're serializing your typed DataSet, I suggest you try to
use WriteXml and write the schema info together with the DataSet xml data.
Then when you deserialize it using ReadXml, also read the schema. Here is a
code sample.

DataSet1 ds = new DataSet1();
ds.CustomProperty = "Value";
ds.WriteXml(@"c:\aaa1.xml", XmlWriteMode.WriteSchema);
DataSet1 ds1 = new DataSet1();
ds1.ReadXml(@"c:\aaa1.xml", XmlReadMode.ReadSchema);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks Kevin. It worked for me now except:

.. The normal serialization does not work.
.. I can't binary serialize the typed dataset with custom public properties.
.. If the public property is Int16, it still does not get serialized.

So I can work around my problem now, but this still suggests that it's not
ideal if not a bug.

Michael
 
Thank you Michael for your feedback. I will forward this to the appropriate
team.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top