By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Inheriting from XSDObjectGen classes and XmlSerialize

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.