Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 12th, 2005, 04:12 AM
Peter Bates
Guest
 
Posts: n/a
Default Inheriting from XSDObjectGen classes and XmlSerialize

Hi,

I'm just getting used to XSDObjectGen and i have the following question.
Can i use a class inherited from a class generated by XSDObjectGen with
XmlSerialize?

Specifically, I have many xml files arriving from a PC inventory scanner we
use.
I wish to deserialize them and then process them.

I've used XSDObjectGen and XmlSerialize.Deserialize to do this, and it
works really well.

However, for the next step I want to create a set of classes that provide
functionality on top of the class produced by XmlSerialize.Deserialize.

I could create these classes and make the XSDObjectGen generated class a
private memeber, but I'd prefer to inherit from the generated class.

Whenever i try to do this, i get exceptions from XmlSerialize.Deserialize.

The code that works is below

XmlSerializer s = new XmlSerializer(typeof(HatLib.HatXml.ScanData));
HatLib.HatPc scanData = (HatLib.HatXml.ScanData)s.Deserialize(new
XmlTextReader(fileName));

However, when i create a class as follows

public class HatPc : HatLib.HatXml.ScanData
{
public HatPc()
{
}
// Specific code here

}

I can't use this class with XmlSerialize.Deserialize. I've tried many
combinations of casting etc to see if i can make it work, but to no avail.

Any suggestions?

Thanks

  #2  
Old November 12th, 2005, 04:12 AM
Christoph Schittko [MVP]
Guest
 
Posts: n/a
Default Re: Inheriting from XSDObjectGen classes and XmlSerialize

You have to be careful with Inheritance and the XmlSerializer.

You have to instantiate the XmlSerializer instance for the derived type,
not the base type. However, you can't deserialize the XML documents of
your base type with that serializer any longer as that serializer
expects a different root element for the derived type.

You're much better off not changing the generated classes or deriving
from them when you need to work with the XmlSerializer. Can you design
classes to operate on the serialization classes instead?

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[color=blue]
> -----Original Message-----
> From: Peter Bates [mailto:PeterBates@discussions.microsoft.com]
> Posted At: Sunday, January 23, 2005 6:05 PM
> Posted To: microsoft.public.dotnet.xml
> Conversation: Inheriting from XSDObjectGen classes and XmlSerialize
> Subject: Inheriting from XSDObjectGen classes and XmlSerialize
>
> Hi,
>
> I'm just getting used to XSDObjectGen and i have the following[/color]
question.[color=blue]
> Can i use a class inherited from a class generated by XSDObjectGen[/color]
with[color=blue]
> XmlSerialize?
>
> Specifically, I have many xml files arriving from a PC inventory[/color]
scanner[color=blue]
> we
> use.
> I wish to deserialize them and then process them.
>
> I've used XSDObjectGen and XmlSerialize.Deserialize to do this, and[/color]
it[color=blue]
> works really well.
>
> However, for the next step I want to create a set of classes that[/color]
provide[color=blue]
> functionality on top of the class produced by[/color]
XmlSerialize.Deserialize.[color=blue]
>
> I could create these classes and make the XSDObjectGen generated[/color]
class a[color=blue]
> private memeber, but I'd prefer to inherit from the generated class.
>
> Whenever i try to do this, i get exceptions from[/color]
XmlSerialize.Deserialize.[color=blue]
>
> The code that works is below
>
> XmlSerializer s = new XmlSerializer(typeof(HatLib.HatXml.ScanData));
> HatLib.HatPc scanData = (HatLib.HatXml.ScanData)s.Deserialize(new
> XmlTextReader(fileName));
>
> However, when i create a class as follows
>
> public class HatPc : HatLib.HatXml.ScanData
> {
> public HatPc()
> {
> }
> // Specific code here
>
> }
>
> I can't use this class with XmlSerialize.Deserialize. I've tried many
> combinations of casting etc to see if i can make it work, but to no[/color]
avail.[color=blue]
>
> Any suggestions?
>
> Thanks[/color]

  #3  
Old November 12th, 2005, 04:13 AM
Peter Bates
Guest
 
Posts: n/a
Default Re: Inheriting from XSDObjectGen classes and XmlSerialize

Yeah,
After looking at it a bit harder I've decided to just construct classes that
hold the deserialized class as a private member.

much easier.

thanks for confirming that.

"Christoph Schittko [MVP]" wrote:
[color=blue]
> You have to be careful with Inheritance and the XmlSerializer.
>
> You have to instantiate the XmlSerializer instance for the derived type,
> not the base type. However, you can't deserialize the XML documents of
> your base type with that serializer any longer as that serializer
> expects a different root element for the derived type.
>
> You're much better off not changing the generated classes or deriving
> from them when you need to work with the XmlSerializer. Can you design
> classes to operate on the serialization classes instead?
>
> HTH,
> Christoph Schittko
> MVP XML
> http://weblogs.asp.net/cschittko
>
>[color=green]
> > -----Original Message-----
> > From: Peter Bates [mailto:PeterBates@discussions.microsoft.com]
> > Posted At: Sunday, January 23, 2005 6:05 PM
> > Posted To: microsoft.public.dotnet.xml
> > Conversation: Inheriting from XSDObjectGen classes and XmlSerialize
> > Subject: Inheriting from XSDObjectGen classes and XmlSerialize
> >
> > Hi,
> >
> > I'm just getting used to XSDObjectGen and i have the following[/color]
> question.[color=green]
> > Can i use a class inherited from a class generated by XSDObjectGen[/color]
> with[color=green]
> > XmlSerialize?
> >
> > Specifically, I have many xml files arriving from a PC inventory[/color]
> scanner[color=green]
> > we
> > use.
> > I wish to deserialize them and then process them.
> >
> > I've used XSDObjectGen and XmlSerialize.Deserialize to do this, and[/color]
> it[color=green]
> > works really well.
> >
> > However, for the next step I want to create a set of classes that[/color]
> provide[color=green]
> > functionality on top of the class produced by[/color]
> XmlSerialize.Deserialize.[color=green]
> >
> > I could create these classes and make the XSDObjectGen generated[/color]
> class a[color=green]
> > private memeber, but I'd prefer to inherit from the generated class.
> >
> > Whenever i try to do this, i get exceptions from[/color]
> XmlSerialize.Deserialize.[color=green]
> >
> > The code that works is below
> >
> > XmlSerializer s = new XmlSerializer(typeof(HatLib.HatXml.ScanData));
> > HatLib.HatPc scanData = (HatLib.HatXml.ScanData)s.Deserialize(new
> > XmlTextReader(fileName));
> >
> > However, when i create a class as follows
> >
> > public class HatPc : HatLib.HatXml.ScanData
> > {
> > public HatPc()
> > {
> > }
> > // Specific code here
> >
> > }
> >
> > I can't use this class with XmlSerialize.Deserialize. I've tried many
> > combinations of casting etc to see if i can make it work, but to no[/color]
> avail.[color=green]
> >
> > Any suggestions?
> >
> > Thanks[/color]
>
>[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,174 network members.