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

Serializing a Collection. Help Please 1

Status
Not open for further replies.

beefeater267

Programmer
Apr 6, 2005
79
Hey,
I have a collection called EmployeeCollection which is derived from a class I made called 'DataCollection'
'DataCollection' is derived from a class I made called 'BaseCollection'
'BaseCollection' is declared:
public abstract class BaseCollection : NameObjectCollectionBase, IEnumerable
In 'BaseCollection' i have:
public string Serialize(System.Type type)
{
MemoryStream stream = new MemoryStream();
XmlRootAttribute root = new XmlRootAttribute(type.Name);
root.Namespace = "XmlSerializer xmlSerializer = new XmlSerializer(type, root);
xmlSerializer.Serialize(stream, this);
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
string xml = sr.ReadToEnd();
sr.Close();
return xml;
}
however, when i call the method:
string x = empColl.Serialize(typeof(EmployeeCollection));
I get an error on line:
XmlSerializer xmlSerializer = new XmlSerializer(type, root);
saying:
File or assembly name bayiduly.dll, or one of its dependencies, was not found.  
I know this error doesn't tell much.. But anyone have any idea what's going on?


Yes, I have a default indexer and Add method.  Please Help. This method works fine on a single Employee object but not on my collection, so it must be something w/ my collection.

Any help out there? Thanks. I'm desparate!
 
When you apply the Serialization attribute to a class, it affects that class only and doesn't make any derived class serializable.
For example, if you derive MyClass from MyBaseClass, MyClass is not serializable:
Code:
[Serializable]
public class MyBase
{
}
public class MyClass: MyBase
{
}
Rule1. If you design a hierarchy and you want to support serialization of any type in the hierarchy, be sure to mark each level with Serializable attribute.
Code:
[Serializable]
public class MyBase
{
}
[Serializable]
public class MyClass: MyBase
{
}
[Serializable]
public class MyOtherClass: MyClass
{
}

If any of the classes in the hierarchy implement ISerializable, there are few design guidelines you have to follow to allow subclasses to provide their own custom serialization and to crrectly manage the custom serialization of the base classes:
- Only the topmost base class that uses custom serialization needs to derive from ISerializable.
- A base class must define its GetObjectData() as virtual to allow subclasses to override it.
- In sublclass implementation of GetObjectData(), the subclass must call its base class implementation of GetObjectData().
- The deserializing constructor shouldn't be private, to allow a subclass to call its base class's deserializing ctor.
- In its implementation of deserializing ctor, the subclass must call its base class
Rule2.
If a base class provides custom serialization, all subclasses derived from it can use only custom serialization.
obislavu

 
Thanks for the reply... I'll give that a shot later in the evening.... None of the classes in my heirarchy implement ISerializable...... is this a problem? I'm not exactly sure what that means to implement ISerializable.


So, you suggest that i place [Serializable] as an attribute of my collection classes / bases?

Thanks again
 
I added [Serializable] to my classes as in the example above and i still get the same error. Anything else it could be?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top