473,320 Members | 1,744 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.

C# App: Serializing collection of derived classes

1
Hi,

My goal is to serialize a collection of type IList<AbstractClass> which contains derived objects instances.
My domain object model is a bit complex so I've created a demmi one:


Expand|Select|Wrap|Line Numbers
  1. public abstract class ProductBase
  2. {
  3.     protected int _id;
  4.     public int ID
  5.     {
  6.         get { return _id; }
  7.         set { _id = value; }
  8.     }
  9.  
  10.     protected string _name;
  11.     public string Name
  12.     {
  13.         get { return _name; }
  14.         set { _name = value; }
  15.     }
  16. }
  17.  
  18. public class Coffee : ProductBase
  19. {
  20.     private string _type;
  21.     public string Type
  22.     {
  23.         get { return _type; }
  24.         set { _type = value; }
  25.     }
  26.  
  27.     private bool _isDecaff;
  28.     public bool IsDecaff
  29.     {
  30.         get { return _isDecaff; }
  31.         set { _isDecaff = value; }
  32.     }    
  33. }
  34.  
  35. public class Tea : ProductBase
  36. {
  37.     private int _numOfSachets;
  38.  
  39.     public int NumOfSachets
  40.     {
  41.         get { return _numOfSachets; }
  42.         set { _numOfSachets = value; }
  43.     }
  44. }
  45.  
  46. public class Store
  47. {
  48.     private List<ProductBase> _products = new List<ProductBase>();
  49.     public List<ProductBase> Products
  50.     {
  51.         get { return _products; }
  52.         set { _products = value; }
  53.     }
  54. }
Let's say the main program doe's the following:

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2. {
  3.     Store store = new Store();
  4.  
  5.     Coffee prod1 = new Coffee();
  6.     prod1.Type = "Espresso";    
  7.  
  8.     Tea prod2 = new Tea();
  9.     prod1.NumOfSachets = 50;    
  10.  
  11.     store.Products.Add(prod1);
  12.     store.Products.Add(prod2);
  13.  
  14.    //TODO: Represent store as XML document
  15.  
  16. }
I Want my XML document to look like this:
<Store>
<Products>
<Coffee type="Espresso" />
<Tea NumOfSachets="50" />
</Products>
</Store>

So far I've done serialization using the XmlSerializer but I can't seem to get it working in here. I've decorated all the attributes and elements of the base and derived classes, but the serialization fails until i add a
Expand|Select|Wrap|Line Numbers
  1. [XmlInclude(typeof(Coffee))]
to the ProductBase class (breaks oop).


Can someone please explain how it's suppose to be done?

Tnx in advance,
Shaul
Dec 3 '07 #1
0 1146

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
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...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
5
by: aa7im | last post by:
I am attempting to create a base collection that I can use to derive strongly typed collection. My base collection class will derive from CollectionBase and I want my typed collections to be...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
1
by: Aidan Glendye | last post by:
Hi, I am trying to store various objects within session. Due to the site being hosted in a web farm we are going to have to use either State Server or SQL Server to persist the data in...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
2
by: she_prog | last post by:
I have a class derived from UserControl. I need to serialize an object of this class, but only some properties of it, as not all properties are serializable (some of the properties coming from...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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....

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.