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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serialization Problem

Status
Not open for further replies.

JBravo

Programmer
Mar 21, 2002
12
IE
Hi All,
I'm stuck with a problem serializing and de-serializing an array. I'm hoping somebody will be able to help me with. I'm trying to serialize an array of my custom class Query to a file. But when I check the file, I can only see one query in there. And when I try to de-serialize it I get an error saying Object doesn't implement IConvertable.
Does anyone have any idea what I am doing wrong? I've put the code below, maybe yee'd like to compile it and check what’s going wrong?
Thanks,
Donal

Code:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Collections;



namespace ProjQueryManager
{
/// 
/// Summary description for Class1.
/// 
class QueryManager
{
public QueryList qList;
string sourceFile = @"C:\source1.dta";

public QueryManager()
{
qList = new QueryList();
}

public bool CheckQuerySourceFile()
{
return true;
}

public void addQuery(Query QueryInfo)
{
qList.Add(QueryInfo);
}

public bool LoadFile()
{
FileStream inStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
StreamReader sReader = new StreamReader(inStream);
SoapFormatter soapReader = new SoapFormatter();
if(qList != null)
qList = null;
qList = (QueryList) soapReader.Deserialize(inStream);
inStream.Close();

foreach(Query q in qList)
q.PrintInfo();
return true;
}


public bool SaveQuerys()
{
FileStream outStream = new FileStream(sourceFile, FileMode.OpenOrCreate, FileAccess.Write);
try
{
SoapFormatter soapWriter = new SoapFormatter();
soapWriter.Serialize(outStream, qList);
outStream.Close();
}
catch(SerializationException sem)
{
outStream.Close();
Console.WriteLine(sem.ToString());
}

return true;
}

public bool SaveQuerys2()
{
try
{
ArrayList temp = new ArrayList();
temp.Add("howdy");
temp.Add("partner");

FileStream outStream = new FileStream(sourceFile, FileMode.OpenOrCreate, FileAccess.Write);
SoapFormatter soapWriter = new SoapFormatter();
soapWriter.Serialize(outStream, temp);
}
catch(SerializationException sem)
{
Console.WriteLine(sem.ToString());
}

return true;
}



/// 
/// The main entry point for the application.
/// 
[STAThread]
static void Main(string[] args)
{
QueryManager myQM = new QueryManager();
Query qi = new Query();

qi.name = "Query 1";
qi.description = "The first Query";
qi.SQL = "Selecr dfg dfg fdsfdsg gfds";
myQM.addQuery(qi);

qi.name = "Query 2";
qi.description = "The second Query";
qi.SQL = "Selecr dfg dfg fdsfdsg gfds";
myQM.addQuery(qi);

qi.name = "Query 3";
qi.description = "The third Query";
qi.SQL = "Selecr dfg dfg fdsfdsg gfds";
myQM.addQuery(qi);




myQM.SaveQuerys();

//myQM.LoadFile();
}
}
}





/// 
/// A struct for storing query information
/// 
[Serializable]
public class Query : ISerializable
{
/// 
/// The name of the query.
/// 
public string name;
/// 
/// A description of the query.
/// 
public string description;
/// 
/// The query string.
/// 
public string SQL;

public Query()
{
}

/// 
/// Constructor
/// 
/// The name of the query.
/// A description of the query.
/// The query string.
public Query(string qname, string qdesc, string qSQL)
{
name = qname;
description = qdesc;
SQL = qSQL;
}

/// 
/// De-serializes a query object
/// 
/// Holds all the data needed to serialize or deserialize an object
/// The destination for this serialization.
public Query(SerializationInfo serInfo, StreamingContext streamContext)
{
name = serInfo.GetString("name");
description = serInfo.GetString("description");
SQL = serInfo.GetString("SQL");
}

/// 
/// Prints the query's attributes to the console.
/// 
public void PrintInfo()
{
Console.WriteLine("name: " + name);
Console.WriteLine("description: " + description);
Console.WriteLine("SQL: " + SQL);
}

/// 
/// Populates a SerializationInfo with the data needed to serialize the target object.
/// 
/// The SerializationInfo to populate with data. 
/// The destination for this serialization. 
public void GetObjectData(SerializationInfo serInfo, StreamingContext streamContext)
{
serInfo.AddValue("name", name);
serInfo.AddValue("description", description);
serInfo.AddValue("SQL", SQL);
}
}








[Serializable()]
public class QueryList : IEnumerable, ICollection, ISerializable
{
protected ArrayList TheQueryList;

/// 
/// Default constructor.
/// 
public QueryList()
{
TheQueryList = new ArrayList();
}

/// 
/// Called when de-serializing a QueryList object.
/// 
/// Holds all the data needed to serialize or deserialize an object
/// The destination for this serialization.
public QueryList(SerializationInfo serInfo, StreamingContext streamContext)
{
try
{

ArrayList aL = new ArrayList();
TheQueryList = (ArrayList)serInfo.GetValue("TheQueryList", aL.GetType());
}
catch(InvalidCastException e)
{
Console.WriteLine("Line 265: " + e.Message + "\n \n \n");
}
}

/// 
/// Adds a Query object to the query list
/// 
public int Add(Query QueryInfo)
{
return TheQueryList.Add(QueryInfo);
}

/// 
/// Populates a SerializationInfo with the data needed to serialize the target object.
/// 
/// The SerializationInfo to populate with data. 
/// The destination for this serialization. 
public void GetObjectData(SerializationInfo serInfo, StreamingContext streamContext)
{
//serInfo.AddValue("TheQueryList", TheQueryList);
serInfo.AddValue("TheQueryList", TheQueryList.GetType());
}

/// 
/// Returns an Query object
/// 
/// The index of the required Query object
/// null if the index is out of range, otherwise the required Query
public Query getQueryAt(int index)
{
if(TheQueryList.Count < index)
return (Query) TheQueryList[index];
else
{
Query temp = new Query(&quot;Index out of range&quot;, &quot;null&quot;, &quot;null&quot;);
return temp;
}
}


/// 
/// Creates an object with the provided parameters and adds it to the Query List.
/// 
/// The name of the query.
/// A description of the query.
/// The query string.
/// The index that the query was added to
public int Add(string name, string description, string statement)
{
Query sQI = new Query(name, description, statement);
return TheQueryList.Add(sQI);
}



/// 
/// This Property is the number of elements in the Array list
/// 
//ICollection.Count
public int Count
{
get
{
return TheQueryList.Count;
}
}

//ICollection.IsSynchronized
public bool IsSynchronized
{
get
{
return TheQueryList.IsSynchronized;
}
}

//ICollection.SyncRoot
public object SyncRoot
{
get
{
return TheQueryList.SyncRoot;
}
}

//ICollection.CopyTo
public void CopyTo(Array dest, int index)
{
TheQueryList.CopyTo(dest, index);
}

/// 
/// This Function clears all elements in the list
/// 
public void Clear()
{
TheQueryList.Clear();
}


//IEnumerable.GetEnumerator
public IEnumerator GetEnumerator()
{
return new QueryListEnumerator(TheQueryList);
}

//IEnumerator Class
public class QueryListEnumerator : IEnumerator
{
ArrayList TheQueryList;
IEnumerator TheEnumerator;

public QueryListEnumerator(ArrayList QLT)
{
this.TheQueryList = QLT;
TheEnumerator = QLT.GetEnumerator();
}

public object Current
{
get
{
return TheEnumerator.Current;
}
}

public bool MoveNext()
{
return TheEnumerator.MoveNext();
}

public void Reset()
{
}
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top