serialization prob

F

frazer

using System;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace MySerialize
{
/// <summary>
/// Summary description for MySerialize.
/// </summary>
[Serializable]
public class MySerialize: ISerializable
{
string name = "Sheraze";
private MySerialize()
{
}
public MySerialize(string name )
{
this.name = name;
}
void ISerializable.GetObjectData(SerializationInfo info,StreamingContext
context)
{
info.SetType(typeof(MySerialize));
}
}
public class MyTestApp
{
[STAThread]
public static void Main()
{
FileStream fs = new FileStream("myserialize.txt", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
MySerialize[] obj1 = {new MySerialize("Name1") , new MySerialize("Name2")};
formatter.Serialize(fs, obj1);
fs.Position = 0;
MySerialize[] obj2 = (MySerialize[]) formatter.Deserialize(fs);
fs.Close();
}
}
}



i get an error on this line saying
MySerialize[] obj2 = (MySerialize[]) formatter.Deserialize(fs);

Additional information: The constructor to deserialize an object of type
MySerialize.MySerialize was not found.
why is that and how do i resolve it?
thnx
 
S

Stuart Gunter

As far as I know, serialization requires a public default constructor,
whereas yours is private.

Maybe give this a try and see what happens.

Can anyone confirm this?
 
F

frazer

yup u are right and it works fine now..when i try to Serialize and
deserialize strings.

but i now want to serialize and deserialize a dataset object.
(UserPreferenceDataSet) that i created myself.
how do i do that?

I am able to serialize using
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("DataSet", this.userPreferenceDataSet);
}

but when i deserialize , i get an error.
public UserPreference(SerializationInfo info, StreamingContext context)
{
this.userPreferenceDataSet = (UserPreferenceDataSet)
info.GetValue("DataSet", this.userPreferenceDataSet.GetType());
}

Exception has been thrown by the target of an invocation.

I think maybe the reason is that info.GetValue doesnt take a dataset type
as a parameter.
i looked into a dataset created internally by dotnet.


protected UserPreferenceDataSet(SerializationInfo info, StreamingContext
context) {
string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
if ((strSchema != null)) {
DataSet ds = new DataSet();
ds.ReadXmlSchema(new XmlTextReader(new System.IO.StringReader(strSchema)));
if ((ds.Tables["UserPreference"] != null)) {
this.Tables.Add(new UserPreferenceDataTable(ds.Tables["UserPreference"]));
}
this.DataSetName = ds.DataSetName;
this.Prefix = ds.Prefix;
this.Namespace = ds.Namespace;
this.Locale = ds.Locale;
this.CaseSensitive = ds.CaseSensitive;
this.EnforceConstraints = ds.EnforceConstraints;
this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
this.InitVars();
}
else {
this.InitClass();
}
this.GetSerializationData(info, context);
System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler =
new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
this.Tables.CollectionChanged += schemaChangedHandler;
this.Relations.CollectionChanged += schemaChangedHandler;
}

do i have to deserialize using this code as a guide?
or is there a simpler way to deserialze a dataset?
thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top