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