How to serialize a base class with derived classes

.

Hello,

I have this code example:

public class Vehicule
{
    public string Name { get; set; }
    public Brand Brand { get; set; }
}
public class Car : Vehicule
{
    public string Matriculation { get; set; }
}



public class Brand
{
    public string Name { get; set; }
}
public class Renault : Brand
{
    public string Information { get; set; }
}

If I create this instance:

var car = new Car { Name = "Clio", Matriculation = "XXX-XXX", Brand = new Renault { Name = "Renault", Information = "Contact Infos" } };

When I serialize this object as follows:

var serializer = new XmlSerializer(typeof(Car), new Type[] { typeof(Renault)});
serializer.Serialize(wr, car);

I get the following:

<?xml version="1.0" encoding="utf-8"?>
<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Clio</Name>
  <Brand xsi:type="Renault">
    <Name>Renault</Name>
    <Information>Contact Infos</Information>
  </Brand>
  <Matriculation>XXX-XXX</Matriculation>
</Car>

But, in my project, I should not have information about derived classes, I would like only the elements of base classes from this instance as follows:

var serializer = new XmlSerializer(typeof(Vehicule));
serializer.Serialize(wr, car);

Xml:

<?xml version="1.0" encoding="utf-8"?>
<Vehicule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Clio</Name>
  <Brand>
    <Name>Renault</Name>
  </Brand>
</Vehicule>

Could you help me get a good Xml (only with the base type of the car and brand)?

Many thanks

+4
source share
2 answers

You cannot magically serialize a derived class as a base because

"... Serialization checks the type of the instance by calling the Object.getType () method. This method always returns the exact type of the object.

http://bytes.com/topic/net/answers/809946-how-force-serialize-base-type

, , IXmlSerializable .

IXmlSerializable: http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable(v=vs.110).aspx

. XML, , : 1) XmlIncludeAttributes , , 2) XmlSerializer, .

Edit: , , Clone() , .

LinqPad:

public class Vehicule
{
    public string Name { get; set; }
    public Brand Brand { get; set; }

    public Vehicule Clone()
    {
        return new Vehicule { Name = this.Name, Brand = new Brand { Name = this.Brand.Name } };
    }
}
public class Car : Vehicule
{
    public string Matriculation { get; set; }
}



public class Brand
{
    public string Name { get; set; }
}
public class Renault : Brand
{
    public string Information { get; set; }
}

void Main()
{   
    var car = new Car { Name = "Clio", Matriculation = "XXX-XXX", Brand = new Renault { Name = "Renault", Information = "Contact Infos" } };
    var vehicle = car as Vehicule;

    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Vehicule));

    XmlWriterSettings settings = new XmlWriterSettings
    {
        Encoding = new UnicodeEncoding(false, false),
        Indent = false,
        OmitXmlDeclaration = false
    };

    using(StringWriter textWriter = new StringWriter()) {
        using(XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) {
            serializer.Serialize(xmlWriter, vehicle.Clone());
        }
        textWriter.ToString().Dump();
    }
}
+2

, imho.

, Contact, ContactSummary. Contact, , ContactSummary , .. Xml Json , [KnownType()] , , .

, , DTO , DTO ?

DTO, , . ...

public class ContactSummary
{
    public string Name { get; set;}
    public string Phone { get; set; }
}

public class Contact
{
    public ContactSummary Summary { get; set; }
    // ... other properties here
}

Car Vehicle - - ...

[KnowTypes(typeof(Renault))]
public class Vehicle
{
    public string Name { get; set; }
    public Brand Brand { get; set; }
}

public class Car
{
    public Vehicle Vehicle { get; set; }
    public string Matriculation { get; set; }
}

, "base" , Car.Vehicle.

+2

All Articles