Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Deserialising xml and Inheritance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johannes Elsinghorst - 08 Sep 2007 17:07 GMT
Hello,
I have a class  "Version" and another two classes "SpezielleVersion1"
and "SpezielleVersion2", which inherit from "Version".
When i deserialize a xml-Instance of "SpezielleVersion1" like this:
XmlSerializer xs = new XmlSerializer(typeof(SpezielleVersion1));
XmlTextReader reader = new XmlTextReader(input);
return (SpezielleVersion1)xs.Deserialize(reader);

i get a proper object in return. But sometimes i dont want to
distinguish between
"SpezielleVersion1" and  "SpezielleVersion2", instead i want to
deserialize them as a"Version":
XmlSerializer xs = new XmlSerializer(typeof(Version));
XmlTextReader reader = new XmlTextReader(input);
return (Version)xs.Deserialize(reader);

For that i annotated the "Version" class with
[XmlInclude(typeof(SpezielleVersion1),XmlInclude(typeof(SpezielleVersion2)]
Unfortunately the deserializer quits with an
IllegalArgumentException("<SpeziellVersion1 xmlns='http://...'> wasnt
expectedt.) when it reads "<zg:SpeziellVersion1... >" in the xml-file.

Does anybody have a clue why it does that?
thanks, Johannes Elsinghorst
John Saunders [MVP] - 08 Sep 2007 17:35 GMT
> Hello,
> I have a class  "Version" and another two classes "SpezielleVersion1"
[quoted text clipped - 20 lines]
> Does anybody have a clue why it does that?
> thanks, Johannes Elsinghorst

Johannes, I have a question just to be clear. Does the failing XML file
succeed if you use "XmlSerializer xs = new
XmlSerializer(typeof(SpezielleVersion1))"?

Do you have the XML namespaces correct? You didn't show that in your post.
Signature

--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Johannes Elsinghorst - 09 Sep 2007 10:37 GMT
Hi John,
thanks for your reply.

> Johannes, I have a question just to be clear. Does the failing XML file
> succeed if you use "XmlSerializer xs = new
> XmlSerializer(typeof(SpezielleVersion1))"?
Yes, i get an object with all the properties of "SpezielleVersion1"
and "Version", so inheritance works 'downwards'.

> Do you have the XML namespaces correct? You didn't show that in your post.
> --
Yes, the namespace conforms with the one specified in the xsd.

regards, Johannes.
Frans Bouma [C# MVP] - 10 Sep 2007 09:03 GMT
> Hello,
> I have a class  "Version" and another two classes "SpezielleVersion1"
[quoted text clipped - 20 lines]
> Does anybody have a clue why it does that?
> thanks, Johannes Elsinghorst

    The XmlSerializer generates C# code under the hood to perform the
serialization/deserialization. As you specified 'Version' as the type,
it will create code which creates a new instance of Version (how else
would it know what to create to store the data in?) and as you want to
have a different type back, this won't work.

    Only exact type matches work with xml serialization as xml
serialization is about data, not about objects. So the data doesn't
represent a type, it represents the data in xml format. that it fits
inside an object of type X or Y isn't important.

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Johannes Elsinghorst - 10 Sep 2007 20:05 GMT
What is the XMLInclude-Annotation used for, if serialization doesn't
work this way?

regards, Johannes.

>         The XmlSerializer generates C# code under the hood to perform the
> serialization/deserialization. As you specified 'Version' as the type,
[quoted text clipped - 6 lines]
> represent a type, it represents the data in xml format. that it fits
> inside an object of type X or Y isn't important.
JS - 10 Sep 2007 20:35 GMT
I had the same problems using serialization a couple of years back and
decided that serialization for saving/loading data was not the right
technology to use (in my case at least).  I ended up writing my own
'serialize/deserialize' which could deserialize into an existing
object.  The main negative with my solution is that I have to write
the serialize/deserialize code for each object.  I wish .Net had
supported deserializing into an existing object.  It would have saved
me a lot of work.
Rohit - 11 Sep 2007 08:56 GMT
> I had the same problems using serialization a couple of years back and
> decided that serialization for saving/loading data was not the right
[quoted text clipped - 4 lines]
> supported deserializing into an existing object.  It would have saved
> me a lot of work.
Rohit - 11 Sep 2007 09:00 GMT
> I had the same problems using serialization a couple of years back and
> decided that serialization for saving/loading data was not the right
[quoted text clipped - 4 lines]
> supported deserializing into an existing object.  It would have saved
> me a lot of work.

The approach you have taken in that also if you design it properly,
you need not do lot of coding.

You can create a abstract base class which has one methods for
deserialization and another for serialization and using reflection you
can get the public properties and do the serialization/
deserialization. Now you need to just inherit this class for your
object classes.Now you need not write the code for serialization/
deserialization in every class.

-Regards
Rohit
Johannes Elsinghorst - 12 Sep 2007 08:55 GMT
Our approach is somewhat different, ew actually dont do serialization.
We first create the schemas and xml
and only need to deserialize them as they are config-files.
So as i see it there is no way to tell the Deserializer about the
inheritance, i have to know of what exact type my xml is
in order to deserialize it?

regards, Johannes.

> The approach you have taken in that also if you design it properly,
> you need not do lot of coding.
[quoted text clipped - 8 lines]
> -Regards
> Rohit
Rohit - 11 Sep 2007 08:56 GMT
On Sep 11, 12:05 am, Johannes Elsinghorst
<JohannesElsingho...@gmail.com> wrote:
> What is the XMLInclude-Annotation used for, if serialization doesn't
> work this way?
[quoted text clipped - 13 lines]
>
> - Show quoted text -

XMLInclude annotation used to include the objects which have
association to the current object. Means if we have a refernce of some
other object which is also a serializabe object then the serialization/
deserialization will include that also.

Serialization/Deserialization is related to data not to an object that
is why inheritanceis not taken care of but the association is taken
care.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.