473,659 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# App: Serializing collection of derived classes

1 New Member
Hi,

My goal is to serialize a collection of type IList<AbstractC lass> 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="5 0" />
</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 1169

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

Similar topics

8
2202
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 the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
0
2488
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 derived classes from the abstract classes The 1st class contains the 2nd class The 2nd class is a custom collection for the 3rd class
2
3291
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 ‘DataItem’ public class DataItem { public DataItem() {}
5
2089
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 forced to implement certain methods: public abstract class MyCollectionBase: CollectionBase { public void Sort() {.... SORT IMPLEMENTATION } public void Filter() {...> FILTER IMPLEMENTATION }
2
2510
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
1
2574
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 session. All of the objects are classes which inherit from a typed dataset. Looking at MSDN I have found the following two (what I believe to be) relevent statements (from article Object
2
3169
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 problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
0
2082
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. moreover, i'm using a factory pattern to control the creation of these classes (see full code below). i'm having a bit of difficulty getting the deserialization process to work smoothly. when i call Save in the Abstract class, the XmlSerializer...
2
697
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 UserControl are like that). When serializing, how could I ignore all the properties coming from the UserControl class? I know there is XmlIgnoreAttribute, but how could I set it to every property of UserControl, as it is not my class? Thank you...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.