Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : XML Serialization



Hollywood
7/20/2004 1:14:55 PM
I have an class (A) that contains an class (B1) of type (B). An class (C),
contained by class (A), contains its own class (B2) of type (B). When I want
to serialize class (A), I want to ignore, programmatically, certain
properties on class (B1), BUT, I want those same properties to not be
ignored when it serializes class (B2) via serializing class (C).

I've set up an XmlAttributeOverride with an XmlAttribute that has the
XmlIgnore flag set to true for each property I don't want exposed and for
type (B). Of course doing this causes the properties to be ignored for both
class (B1) and (B2).

How do I get around this?


Hollywood
7/20/2004 1:21:24 PM
Also... if I have a class (A) which is derived from class (B) and both
classes are in an ArrayList that's being deserialized, is it possible to
ignore properties that reside in class (B) for only class (A)? i.e. IsB
would be ignored for class A, but would be serialized for class B.

class A : B
{
public bool IsA
{
get { return true; }
}
}

class B
{
public bool IsB
{
get { return true; }
}
}

Dino Chiesa [Microsoft]
7/27/2004 9:19:52 PM
[quoted text, click to view]

use a different override in each case.


Dino Chiesa [Microsoft]
7/27/2004 9:44:16 PM
You may be able to do what you want with a XxxSpecified field, eg

public class DerivedType1: BaseType {
public int PropertyRelevantToDerivedInstances;
}

public class DerivedType2: BaseType {
public int PropertyRelevantToEverybody;
}

public class BaseType {
public int PropertyNotRelevantToDerivedType1;

[XmlIgnore]
public bool PropertyNotRelevantToDerivedType1Specified {
get {
return !(this is DerivedType1);
}
set {}
}
}


Using this hierarchy of classes, the PropertyNotRelevantToDerivedType1 will
be serialized from instances of the BaseType or DerivedType2, but not from
instances of DerivedType1.

working example:
http://www.winisp.net/cheeso/srcview.aspx?dir=xml-serialization&file=Conditional.cs

-Dino



[quoted text, click to view]

AddThis Social Bookmark Button