467,921 Members | 1,385 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

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

Nov 12 '05 #1
  • viewed: 1990
Share:
2 Replies
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

-----Original Message-----
From: Peter Bates [mailto:Pe********@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 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


Nov 12 '05 #2
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:
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

-----Original Message-----
From: Peter Bates [mailto:Pe********@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

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


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pavan | last post: by
1 post views Thread by SteveC | last post: by
reply views Thread by Pascal Brunot | last post: by
reply views Thread by William | last post: by
reply views Thread by Jeff Killberg | last post: by
1 post views Thread by Veerle | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.