473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how to ignore inherited members with XMLSerialization

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!

Nov 23 '05 #1
6 7686
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********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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 : 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!

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********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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 : 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!

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********@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.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
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...
4
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...
0
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
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...
6
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...
2
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...
14
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...
19
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.