Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / February 2005

Tip: Looking for answers? Try searching our database.

how to ignore inherited members with XMLSerialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rob.bowley@gmail.com - 12 Feb 2005 15:28 GMT
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!

Please help!
Dino Chiesa [Microsoft] - 14 Feb 2005 23:14 GMT
How about using an Adapter pattern?

http://c2.com/cgi-bin/wiki?AdapterPattern

Don't serialize the class, serialize the adapter class.

public class DetailsAdapter {

[XmlIgnore]
public string Id;

// other fields/props here

public Details ToDetails() {
    // produce  a Details instance here
}

}

-Dino

>I have a class which inherits from a generated abstract base class.
>
[quoted text clipped - 35 lines]
>
> Please help!
rob.bowley@gmail.com - 17 Feb 2005 17:21 GMT
Is that not the same as a wrapper?
Dino Chiesa [Microsoft] - 14 Feb 2005 23:17 GMT
another thing you can try is including

[XmlIgnore]
public bool IdSpecified ;

in the derived class.

In .NET's XML Serialization, a public bool with a name of XxxxSpecified
indicates whether the public prop/field of name Xxxx is specified and should
be serialized.
see here:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemXmlSerializationXm
lSerializerClassTopic.asp

(scroll that page down)

This may not work when you stuff the XxxSpecified field into a derived type;
I haven't tried it.

-Dino

>I have a class which inherits from a generated abstract base class.
>
[quoted text clipped - 35 lines]
>
> Please help!
rob.bowley@gmail.com - 17 Feb 2005 16:54 GMT
Interesting - I'll give it a try when I have a chance and post back

Thanks Dino
rob.bowley@gmail.com - 18 Feb 2005 12:17 GMT
Hi Dino,

I have tried your second solution and it works - 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.

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
Dino Chiesa [Microsoft] - 22 Feb 2005 22:27 GMT
Glad to hear you succeeded.
This gets better in .NET 2.0, and better again in Indigo.
Another approach is to use attribute overrides.  You can dynamically
generate attributes to hide and expose what you want.
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlserializationxm
lattributeoverridesclasstopic.asp


I can imagine reflecting on all the properties of a type (inherited or not)
and applying attributes on each one. . .
-D

> Hi Dino,
>
[quoted text clipped - 20 lines]
>
> Rob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.