473,320 Members | 2,004 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,320 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 1824

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.