473,611 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to ignore inherited members with XMLSerializatio n

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 : GeneratedDetail s
{
[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!

Nov 23 '05 #1
6 7708
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
<ro********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 : GeneratedDetail s
{
[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!

Nov 23 '05 #2
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...ClassTopic.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
<ro********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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 : GeneratedDetail s
{
[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!

Nov 23 '05 #3
Interesting - I'll give it a try when I have a chance and post back

Thanks Dino

Nov 23 '05 #4
Is that not the same as a wrapper?

Nov 23 '05 #5
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

Nov 23 '05 #6
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...classtopic.asp

I can imagine reflecting on all the properties of a type (inherited or not)
and applying attributes on each one. . .
-D
<ro********@gma il.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
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

Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5568
by: sam | last post by:
Hi Doesn't seem like XMLSerialization serializes Shared properties. My vb.net class has a load of properties, only two of which are marked as Shared. When I serialize it to XML, all the properties are represented in the resulting XML except the two shared members. Can't find anything on MSDN to confirm this.
4
4090
by: rob bowley | last post by:
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 {
0
1846
by: Jax | last post by:
Just to confirm. If I have object foo: public class foo { private string myPrivate; protected string myProtected; public string myPublic; public class foo()
1
2046
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned form) to the project via the IDE However, when I drop down the "(base class events)" list, I can't find the methods defined in the interface. I know the interface has been inherited because if I enter an "Implements" statement in the derived...
6
1759
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
2
2971
by: lovecreatesbeauty | last post by:
I'm disturbed on this question on a long time. I think if I finally get understand it with your kind help, I will get close to a excellent C++ programmer. And I only can rely on your expertise and help. I've read some books, but no book focuses on this. Don't you think it is a very important thing of C++ programming language? So your knowledge is of great benefit to others, especially me. 1. Which (How many) members will be created...
14
2621
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
19
2223
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
2
1946
by: eggie5 | last post by:
Hi, I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog called boxer? Just get the Dog members from bulldog and leave out any inherited members from Animal. Dog bulldog=new Dog(); <-has inhereited members from Animal
0
8149
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8596
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8561
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8240
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8411
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.