all groups > dotnet xml > february 2005 >
You're in the

dotnet xml

group:

how do you ignore inherited members with XMLSerialization?


how do you ignore inherited members with XMLSerialization? rob.bowley NO[at]SPAM gmail.com
2/12/2005 7:21:47 AM
dotnet xml:
I have a class which inherits from a generated abstract base class.

I simply want to hide some fields which are inherited from the base
class when it is serialised.

I have tried:

public class Details : GeneratedDetails
{
[XmlIgnore]
public new string Id
{
get{return base.lId;}
set{Id = value;}
}

and:

[XmlIgnore]
public override string Id
{
get{return base.lId;}
set{Id = value;}
}
}

All these do is serialise the base members instead. As the base class
is generated it is no good putting the [XMLIgnore] on the base class
members (although this is what I have had to do for now).

I have Googled extensively but found no examples or answers to this
problem - apart from creating a wrapper class which inherits from my
Details class and only exposes the members I want my visible in the
web service. That will create an immense amount of work as it will
mean replicating a load of methods and derived classes of the Details
class just for the web service and therefore defeating the object of
OO!

Re: how do you ignore inherited members with XMLSerialization? Derek Harmon
2/12/2005 9:55:01 PM
[quoted text, click to view]

Why not generate the base class with the XmlIgnoreAttributes on
the base class members? It sounds like you have the source code
to do this, and it's only a text file so it's pretty straightforward to
insert a step in the generation process taking the source code w/o
XmlIgnoreAttribute and adding the XmlIgnoreAttribute auto-
matically.


Derek Harmon

Re: how do you ignore inherited members with XMLSerialization? rob.bowley NO[at]SPAM gmail.com
2/13/2005 1:33:06 AM
Hi Derek,

Thanks for replying.

I could do that, but then all generated base class members would be
ignored by default and I would then have to override every member I
wished to expose, which would often be quite a lot. It also would mean
re-engineering all existing code that inherits from classes generated
using the template.

However, that is better than what I'm doing at the moment and better
than creating a wrapper.

What I really want to clear up is if there is any way at all of telling
the serializer to ignore members from a base class.
Re: how do you ignore inherited members with XMLSerialization? Chris Walls
2/16/2005 8:48:48 AM
I have the same issue with a web service. A property on a base class is an
IDictionary type which cannot be serialized. Without modifying the base
class' source code, how can I tell the XmlSerializer (and SoapSerializer for
that matter) to ignore this property? I too attempted to override the
property and mark it with XmlIgnore, but obviously that did not work.

Thanks,
Chris

[quoted text, click to view]

Re: how do you ignore inherited members with XMLSerialization? rob.bowley NO[at]SPAM gmail.com
2/18/2005 4:19:21 AM
I now have a few solutions to this proplem.

I created a boolean - IdSpecified - in the base class set to true and
then set it to false in the constructor of the derived class. This hid
the Id property from serialisation as the serialiser recognises this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlSerializationXmlSerializerClassTopic.asp

However, the solution we are going with is to have all base members set
to XmlIgnore and then override them in the derived class. As the body
of the derived class is also initially generated, all fields will be
overriden so that they are exposed. Then, to hide a member, we simply
delete it from the derived class.

This also helps with changing a property's type in the derived class
(serialisation fails if a property is hiding an inherited property with
a different type) as it will not build unless you delete the generated
overriden property in the derived class.

I still think you should be able to hide an inherited property from the
derived class without manipulating the base class.

Thanks,

Rob
AddThis Social Bookmark Button