Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get rid of XmlInclude when using XmlSerializer

0.00/5 (No votes)
29 Jul 2011 1  
How would you do this, if objects included inside one of the serialized objects was an abstract class? E.g.:abstract class Base{ public int x;}class A : Base{ public int y;}class B : A{ public int z;}class SomeOtherClass{ public string...

How would you do this, if objects included inside one of the serialized objects was an abstract class? E.g.:


C#
abstract class Base
{
    public int x;
}

class A : Base
{
    public int y;
}

class B : A
{
    public int z;
}

class SomeOtherClass
{
    public string s;
}

class SerializingClass
{
    public int w;
    public Base myB1;
    public Base myB2;
}

//AND you have an object
var sc = new SerializingClass{
   w = 10,
   myB1 = new B{ x = 1, y, 2, z = 3},
   myB2 = new A{ x = 10, y=20 }
};

How would you use your method to add just Base, A, B, and not SomeOtherClass without knowing the inner classes and derived classes held within your top level class which you request the serializer from? If you must know all of this, then I'd feel that this method is limited to shallow class structs and the XML attribute method works better (for this case).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here