473,414 Members | 1,781 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,414 software developers and data experts.

Serialization of an Inherited Class.!

I am new to serialization. Apologies, if my question sounds really
basic. Stuck in this problem for the last two hours. Thought, will ask
the experts instead of banging my head more.

This is what I have..!

<code>

Abstract Class FooBase
{

}

Abstract Class FoobaseCollection : ICollection, IEnumerable
{
private ArrayItem fooBaseList;

public foobase this[int index]
{
get
{
...
...
}
}

public void Add(FooBase object)
{
....
}
}

[XmlRoot("Foobar")]
Class Foobar : Foobase
{

}

[XmlRoot("FoobarCollection")]
Class FoobarCollection : FoobaseCollection
{

}
</code>

When I serialize the object using

<code>

FoobarCollection foobarCollection = new FoobarCollection();
foobarCollection.Add(new FooBase());
XmlSerializer serializer = new
XmlSerializer(typeof(FoobarCollection), new Type[] {typeof(Foobar)});
XmlTextWriter xmlTextWriter = new XmlTextWriter(@"C:\foobar.xml",
System.Text.Encoding.Default);
serializer.Serialize(xmlTextWriter, foobarCollection);
</code>

I dont get the expected xml. The internal arraylist elements are
serialized into tags with base class types(FooBase) rather than its
own type(FooBar).

eg:

<xml>

<FoobarCollection>
<FooBase ... />
<FooBase ... />
<FooBase ... />
</FoobarCollection>

instead of

<FoobarCollection>
<Foobar... />
<Foobar ... />
<Foobar ... />
</FoobarCollection>

</xml>
Am I missing something here. I cannot add the XmlElement or
XmlArrayList attribute to the base class property(public foobase
this[int index]), as I dont want to hardcode the type into some
inherited class name.

Is this a limitation in the pattern I have implemented?

Thanks!
-Karthick R

Mar 1 '07 #1
1 1389
R Karthick <ka*******************@gmail.comwrote:
I am new to serialization. Apologies, if my question sounds really
basic. Stuck in this problem for the last two hours. Thought, will ask
the experts instead of banging my head more.
<snip>
When I serialize the object using

<code>

FoobarCollection foobarCollection = new FoobarCollection();
foobarCollection.Add(new FooBase());
XmlSerializer serializer = new
XmlSerializer(typeof(FoobarCollection), new Type[] {typeof(Foobar)});
XmlTextWriter xmlTextWriter = new XmlTextWriter(@"C:\foobar.xml",
System.Text.Encoding.Default);
serializer.Serialize(xmlTextWriter, foobarCollection);
</code>
Um, that's adding a new FooBase, not a Foobar - in other words, it
shouldn't be trying to serialize it as a FooBar. However, it's hard to
know whether that's just a bug in your sample code or not.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '07 #2

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

Similar topics

0
by: ron | last post by:
This is as clean as i can get it. Thanks Ron This is the base class that serializes and desrializes the class objects it also provides abstract methodes that are called from its inherited...
1
by: Maheal | last post by:
I have been trying to Serialize an object that is the child of another object which is also serializable. Here is the simplified scenario (assume missing code is correct): class One :...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
2
by: Ilya Evseev | last post by:
Hi, folks! There is some class in external library that is declared with attribute. I need to create class inherited from them that should be serializable too. My class uses fields calculated...
1
by: David C | last post by:
I am new to Serialization, so please bear with me. I have a class inherited from a non-serializable class. My goal is to serialize an instance of it to XML and I am only interested in the...
1
by: Glenn Wilson | last post by:
In the current project that I am planning i am thinking of serializing the objects to save as binary files. The question I have is with inheriting classes. If I have 1 base class and 2 classes...
2
by: tony lock | last post by:
I have a class inherited from Control, which I want to serialize, since Control is not Serializable, I have had to implement ISerializable. This works but I now want to inherit this base class into...
2
by: Kevin Burton | last post by:
Hopefully this is the right group. I am getting the following exeption: Member 'PPRP602AType.RPLY_HEADER' hides inherited member 'REPLYType.RPLY_HEADER' , but has different custom attributes....
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.