473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with serializing multiple collections with a base class

I have the following code. When I serialize the DriverCollection I would
like to get both the Driver Collection and the DeletedItems collection. I
end up with only the Driver collection. I have tried numerous attributes
with no success. Any help would be appreciated...
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;

namespace SerialTest
{
#region BusinessObjectBase
/// Summary description for BusinessObjectBase.
/// </summary>
[Serializable()]
public abstract class BusinessObjectBase
{
public BusinessObjectBase()
{}
}
#endregion //BusinessObjectBase

#region BusinessObjectCollectionBase
/// <summary>
/// Summary description for BusinessObjectCollectionBase.
/// </summary>
[Serializable()]
public class BusinessObjectCollectionBase : CollectionBase
{
public DeletedItemCollection _deletedItems = new DeletedItemCollection();

public BusinessObjectCollectionBase()
{}

public DeletedItemCollection DeletedItems
{
get{return _deletedItems;}
set{_deletedItems = value;}
}
}
#endregion //BusinessObjectCollectionBase

#region DeletedItemCollection
/// <summary>
/// Summary description for BusinessObjectCollectionBase.
/// </summary>
[Serializable()]
public class DeletedItemCollection : CollectionBase
{
#region Constructors

public DeletedItemCollection()
{}

#endregion

public BusinessObjectBase this[int index]
{
get{return (BusinessObjectBase)List[index];}
set{List[index] = value;}
}

public int Add(BusinessObjectBase value)
{
return List.Add(value);
}

}
#endregion //DeletedItemCollection

#region DriverCollection
/// <summary>
/// Summary description for DriverCollection.
/// </summary>
[Serializable()]
public class DriverCollection : BusinessObjectCollectionBase
{
#region Constructors

public DriverCollection()
{}

#endregion

#region IListMethods

public Driver this[int index]
{
get{return (Driver)List[index];}
set{List[index] = value;}
}

public int Add(Driver value)
{
return List.Add(value);
}

public bool Contains(Driver value)
{
return List.Contains(value);
}

public bool Contains(int driverNumber)
{
Driver d = GetDriver(driverNumber);
return d == null ? false : Contains(d);
}

public bool ContainsDeleted(int driverNumber)
{
foreach(BusinessObjectBase bob in _deletedItems)
{
Driver d = (Driver)bob;
if(d.Number == driverNumber)
return true;
}

return false;
}

public int IndexOf(Driver value)
{
return List.IndexOf(value);
}

public int IndexOf(int driverNumber)
{
Driver d = GetDriver(driverNumber);
return d == null ? -1 : IndexOf(d);
}

public void Remove(Driver value)
{
List.Remove(value);
}

public Driver GetDriver(int driverNumber)
{
for(int i = 0; i < this.Count; i++)
if(this[i].Number == driverNumber)
return this[i];

return null;
}

public void DeleteDriver(int driverNumber)
{
Driver d = GetDriver(driverNumber);
Remove(d);
_deletedItems.Add(d);
}

#endregion
}
#endregion //DriverCollection

#region Driver
/// <summary>
/// Summary description for Driver.
/// </summary>
[Serializable()]
public class Driver : BusinessObjectBase
{
#region Constructors

public Driver()
{
}

#endregion
#region Number

int _number = 0;
public int Number
{
get{return _number;}
set
{
if(value != _number)
{
_number = value;
}
}
}

#endregion

}
#endregion //Driver

/// <summary>
/// Summary description for Class1.
/// </summary>
class Tester
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DriverCollection drivers = new DriverCollection();
Driver driver1 = new Driver();
Driver driver2 = new Driver();
Driver driver3 = new Driver();
Driver driver4 = new Driver();
driver1.Number = 1;
drivers.Add(driver1);
driver2.Number = 2;
drivers.Add(driver2);
driver3.Number = 3;
drivers.Add(driver3);
driver4.Number = 4;
drivers.Add(driver4);

drivers.DeleteDriver(2);
drivers.DeleteDriver(3);

//****************************
// XML serialization
//****************************
// Creat a Serializer
try
{

XmlSerializer serializer = new XmlSerializer(typeof(DriverCollection));

// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(@"C:\NewPolicy.xml", FileMode.Create);
XmlWriter writer =
new XmlTextWriter(fs, Encoding.Unicode);

// Serialize using the XmlTextWriter.
serializer.Serialize(writer, drivers);
writer.Close();
}
catch (System.Exception e)
{
throw e;
//Handle exception here
}
}
}
}

--
Gonzo
Nov 12 '05 #1
0 1829

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
0
by: okinrus | last post by:
Can someone take a look at this code and figure out why Serializable_base::add_serializer throws std::bad_alloc. The problem seems to be the compiler because msvc++ 7.1 says...
0
by: Gonzo | last post by:
I have the following code. When I serialize the DriverCollection I would like to get both the Driver Collection and the DeletedItems collection. I end up with only the Driver collection. I have...
0
by: big A | last post by:
I am receiving an error stating that File or Assembly name <filname.dll>, or one of its dependencies, was not found In one assembly I have three abstract classes In another I have three...
1
by: jeff | last post by:
I am using .NET remoting with a class,call it MyClass, that contains many other classes (say one of them is MySubClass). I have been able to successfully call functions from the MyClass, but when...
4
by: Mark Sizer | last post by:
Hi people, I'm having trouble trying to achieve something in C# and win forms, and am looking for a little advice if anyone has a moment. I've got two classes: 1) a base class 2) a derived...
0
by: Kenneth Baltrinic | last post by:
Is the following code correct for serializing a quasi-single class, that is a class that has a descreet (though more than one, so not a true singleton) number of static instances and no dynamically...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
1
by: Karthik1979 | last post by:
I have a custom class inherited from List<T> collection. Along with the base class functionality, I have included my additional properties. When serializing, only the base class items are serialized...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.