Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Serialize a Dataset of custom objects?

Status
Not open for further replies.

tpremore

Technical User
Apr 13, 2001
22
US
What I'm trying to do is serialize and deserialize a dataset which contains table of "serialItem" objects that I have defined as serializable. When I look at the contents of the table in the debugger before writing it clearly contains
SerializationTest.Form1.serialItem objects, but then looking at the deserialized table they are all just strings! I can't use them anymore that way :) Why is this happening?

The code is below - I can't figure out what is wrong, but I am new to serialization and XML so maybe I'm missing a step?

Please provide any insight at all that you may have -
Thank you so much,
tpremore@ideorlando.org




public Form1()
{
InitializeComponent();


set1 = new DataSet("Set1");
set2 = new DataSet("Set2");

dataGrid1.DataSource = set1;
dataGrid2.DataSource = set2;

dt_playbackItems = new System.Data.DataTable("Playback Items (Element References)");
dt_playbackItems.Columns.Add("playbackItem", typeof(serialItem));

dt_playbackItems_read = new System.Data.DataTable("Playback Items read");
dt_playbackItems_read.Columns.Add("playbackItem", typeof(serialItem));

set1.Tables.Add(dt_playbackItems);
set1.Tables.Add(dt_playbackItems_read);


for (int i=0; i<5; i++)
{
serialItem item = new serialItem();
item.num = i*100;;
item.name = "Name " + i.ToString();
System.Data.DataRow row = dt_playbackItems.NewRow();
row["playbackItem"] = item;

dt_playbackItems.Rows.Add(row);

}

string fname = @"C:\tmp\SerializationTest\out.xml";

System.Xml.XmlTextWriter writer = new XmlTextWriter(fname, System.Text.Encoding.Default);

set1.WriteXml(writer, System.Data.XmlWriteMode.WriteSchema);

writer.Close();


System.Xml.XmlTextReader reader = new XmlTextReader(fname);

set2.ReadXml(reader, System.Data.XmlReadMode.ReadSchema);

reader.Close();
}

private class serialItem : ISerializable
{
public int num;
public string name;

void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("num", num);
info.AddValue("name", name);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top